Jump to content

Daz

EstablishedMember
  • Content Count

    74
  • Joined

  • Last visited

Community Reputation

0 Neutral

About Daz

  • Rank
    Regular
  1. Hi everyone, After a couple of years I am re-visiting my temperature conversion headache. The PIC is an 18F2525 @ 4MHz. dq is the I/O pin on RB0. The code below works to the point where the value displayed on the lcd is a large non sensical number that fluctuates. The problem I have is with the conversion formula (I have looked at and tried many versions found on the 'net'). It may be that I cannot use the 'float' command as my version of sourceboost ide doesn't support it (v6.40). Could one of you clever people take a look at my code and suggest a way around this problem. Many thanks in advance. volatile bit dq @ PORTB.0; volatile bit pin @ TRISB.0; unsigned char presence; unsigned char i; int k; unsigned short tempL, tempH, fraction,temp; int data[12]; unsigned char reset(void); unsigned char read_byte(void); unsigned int signbit; #define skip_rom 0xCC #define convert_T 0x44 #define read_scratchpad 0xBE void write_byte(char val); void Read_Temperature(void); //*********************************** unsigned char reset(void) { pin = 0; dq = 0; delay_10us(48); pin = 1; delay_10us(6); presence = dq; delay_10us(48); return (presence);//not needed //0 = part, 1 = no part } //************************************ unsigned char read_byte(void) { char i; char result = 0; pin = 1; for(i=0;i<8;i++) { pin = 0; dq = 0; asm nop; asm nop; pin = 1; if(dq!=0) { result |= 1<<i; delay_10us(6); } } return result; } //*************************** void write_byte(char val) { char i; pin = 1; for(i=0;i<8;i++) { if((val & (1<<i))!= 0) { //write 1 pin = 0; dq = 0; asm nop; pin = 1; delay_10us(6); } else { //write 0 pin = 0; dq = 0; delay_10us(6); pin = 1; } } } //**************************** void Read_Temperature(void) { reset(); if(presence) { lcd_gotoxy(0,0); lprintf("NO SENSOR "); } write_byte(skip_rom); write_byte(convert_T); delay_ms(750); reset(); write_byte(skip_rom); write_byte(read_scratchpad); // Receive 9 bytes for ( i = 0; i < 9; i++) { data = read_byte(); } temp = data[0] + data[1] * 256;//here's where the problem lies! signbit = temp & 0b100000000;//test most sig. bit if(signbit)//negative reading { temp = (temp ^ 0xffff + 1);//2's comp. } } void main( void ) { //***Interrupts intcon = 0b00000000; intcon2 = 0b10000000; intcon3 = 0b00000000; delay_ms(500); lcd_setup(); lcd_gotoxy(0,0); lprintf("TEMPERATURE READING "); lcd_gotoxy(0,1); lprintf(" DS18B20 "); delay_s(5); //Endless loop while( 1 ) { delay_s(1); Read_Temperature(); lcd_gotoxy(0,0); lprintf("T=%d ",temp); }//while }//main
  2. Thanks for your replies. A fundamental error on my part using rb1 as a switch input instead of rb0, the simulator now works. In the while loop this code amendment below works correctly. I hope these posts will serve to help others with similar problems. //Endless loop while( 1 ) { if(flag1) { led1 = !led1; //on/off flag1 = !flag1; //reset } }//while
  3. Hi, I've been away from the programming arena for some time now and I'm just on a personal re-fresher course. Please find below a program that uses 1 switch input to turn on an led. The program doesn't work when I run it in the sourceboost debugger. The breakpoint set in the ext int routine is never reached and therefore the led never lights, it may be obvious to you experienced guys out there any pointers would be apreciated. Thanks in advance. #include <system.h> //Target PIC16F877 configuration word #pragma DATA _CONFIG, _PWRTE_ON & _BODEN_OFF & _WDT_OFF & _LVP_OFF & _CPD_OFF & _DEBUG_OFF & _XT_OSC & _CP_OFF //Set clock frequency #pragma CLOCK_FREQ 4000000 #define button1 portb.1 #define led1 portd.0 unsigned char flag1; void interrupt( void ) { //Handle external interrupt if( intcon & (1<<INTF) ) { if(button1){flag1 = 1;} clear_bit( intcon, INTF ); } //Handle timer0 interrupt if( intcon & (1<<T0IF) ) { clear_bit( intcon, T0IF ); //clear timer 0 interrupt bit } //Handle timer1 interrupt if( pir1 & (1<<TMR1IF) ) { clear_bit( pir1, TMR1IF ); //clear timer 1 interrupt bit } //Handle timer2 interrupt if( pir1 & (1<<TMR2IF) ) { clear_bit( pir1, TMR2IF ); //clear timer 2 interrupt bit } } void main( void ) { //Configure port A trisa = 0x00; //Configure port B trisb = 0x02; //Configure port C trisc = 0x00; //Configure port D trisd = 0x00; //Configure port E trise = 0x00; //Configure A/D pins adcon1 = 0x06; //Initialize port A porta = 0x00; //Initialize port B portb = 0x00; //Initialize port C portc = 0x00; //Initialize port D portd = 0x00; //Initialize port E porte = 0x00; //Set Timer0 mode clear_bit( option_reg, T0CS ); //configure timer0 as a timer //Set prescaler assignment clear_bit( option_reg, PSA ); //prescaler is assigned to timer0 //Set prescaler rate clear_bit( option_reg, PS2 ); //prescaler rate 1:2 clear_bit( option_reg, PS1 ); clear_bit( option_reg, PS0 ); //Set timer0 source edge selection set_bit( option_reg, T0SE ); //increment on high-to-low transition on RA4/T0CKI pin //Set timer 1 prescaler rate clear_bit( t1con, T1CKPS1 ); //prescaler rate 1:1 clear_bit( t1con, T1CKPS0 ); //Set timer 1 mode clear_bit( t1con, TMR1ON ); //disable timer 1 //Set timer 2 prescaler rate clear_bit( t2con, T2CKPS1 ); //prescaler rate 1:1 clear_bit( t2con, T2CKPS0 ); //Set timer 2 postscaler rate clear_bit( t2con, TOUTPS3 ); //postscaler rate 1:1 clear_bit( t2con, TOUTPS2 ); clear_bit( t2con, TOUTPS1 ); clear_bit( t2con, TOUTPS0 ); //Set timer 2 mode (enable or disable) clear_bit( t2con, TMR2ON ); //enable timer 2 //Enable interrupts (External, Timer0) intcon = 0xB0; //Endless loop while( 1 ) { if(flag1) { led1 = 1; } }//while }//main
  4. Hi IanM, At last someone who understands english. I will do as you've suggested, thankyou. Regards, Daz.
  5. The A2D has been performed and I want to display the result.
  6. Hi, Probably a simple answer to this. I have comms using an rs232 line driver to the pc (sourceboost terminal) displaying letters numbers and strings, so far so good. I want to display the results of an a2d conversion, any clues please. Regards, Daz.
  7. Still cannot get it working. Does anybody have a working version of code for a2d conv. on a 12f675 that I can compare to?
  8. Thanks for replying so promptly, it really helps to have another pair of eyes look at a problem. I will change the code as you've suggested and hopefully get some good results. p.s. Dave, Pavel will the IDE simulator have the facility to simulate 12f*** pics' soon?
  9. Lets try it this way. #include <system.h> //Target PIC12F675 configuration word #pragma DATA _CONFIG, _PWRTE_ON & _BODEN_OFF & _WDT_OFF & _CPD_OFF & _INTRC_OSC_NOCLKOUT & _CP_OFF //Set clock frequency #pragma CLOCK_FREQ 4000000 //Outputs volatile bit gp0@GPIO.0;//analog i/p volatile bit gp1@GPIO.1;//analog i/p volatile bit gp2@GPIO.2;//motor drive volatile bit gp4@GPIO.4;//enable volatile bit gp5@GPIO.5;//motor drive #define motor_clockwise gp2 //pointer travels up #define motor_anticlockwise gp5 //pointer travels down #define enable gp4 unsigned char AD_Result; void a2d (char); void interrupt( void ) { //Handle timer0 interrupt if( intcon & (1<<T0IF) ) { clear_bit( intcon, T0IF ); //clear timer 0 interrupt bit } //Handle timer1 interrupt if( pir1 & (1<<TMR1IF) ) { clear_bit( pir1, TMR1IF ); //clear timer 1 interrupt bit } } void main( void ) { //Configure GPIO port trisio = 0x03;//gpio0 & gpio1 as a2d input. gpio = 0x00; cmcon = 0;//comparator off. //ansel = 0; //Set Timer0 mode clear_bit( option_reg, T0CS ); //configure timer0 as a timer //Set prescaler assignment clear_bit( option_reg, PSA ); //prescaler is assigned to timer0 //Set prescaler rate clear_bit( option_reg, PS2 ); //prescaler rate 1:2 clear_bit( option_reg, PS1 ); clear_bit( option_reg, PS0 ); //Set timer0 source edge selection set_bit( option_reg, T0SE ); //increment on high-to-low transition on RA4/T0CKI pin //Set timer 1 prescaler rate clear_bit( t1con, T1CKPS1 ); //prescaler rate 1:1 clear_bit( t1con, T1CKPS0 ); //Set timer 1 mode clear_bit( t1con, TMR1ON ); //disable timer 1 //Enable interrupts (Timer0) intcon = 10100000b; a2d(AD_Result);//pot i/p. //Endless loop while( 1 ) { enable = 1; if(AD_Result>5)//arbitory value to prove a2d returning sensible value. { motor_clockwise = 0; motor_anticlockwise = 1; motor_clockwise = 1; motor_anticlockwise = 0; } }//while }//main //////////////////////////////////////////////////////////////////////////////////////////// void a2d (char AD_Result) { clear_bit(ansel,ADCS2); set_bit(ansel,ADCS1); clear_bit(ansel,ADCS0); clear_bit(ansel,ANS3); clear_bit(ansel,ANS2); clear_bit(ansel,ANS1); set_bit(ansel,ANS0); set_bit(adcon0,ADFM); // Turn on the A/D converter, select channel 0. clear_bit(adcon0,VCFG); clear_bit(adcon0,CHS1); set_bit(adcon0,CHS0); set_bit(adcon0,ADON); set_bit(gpio,gp0); volatile bit Go_Done@ADCON0.1; volatile unsigned char ad_h@ADRESH; volatile unsigned char ad_l@ADRESL; unsigned int shiftH; unsigned int shiftL; set_bit(adcon0,Go_Done); // Start conversion delay_10us(2); shiftH = ad_h; shiftL = ad_l; AD_Result = (shiftH<<2) | (shiftL>>6); // Combine the two registers to get full 10 bits. adcon0 = 0; // Shut off the AD converter. }
  10. Hi, I am trying to get the a2d to work with a 12f675. Has anyone any experience with this pic? I have simulated the a2d using a 16f877 with sourceboost IDE, but unfortunately cannot simulate with the 12f675. Can anyone see where I'm going wrong. CODE #include <system.h> //Target PIC12F675 configuration word #pragma DATA _CONFIG, _PWRTE_ON & _BODEN_OFF & _WDT_OFF & _CPD_OFF & _INTRC_OSC_NOCLKOUT & _CP_OFF //Set clock frequency #pragma CLOCK_FREQ 4000000 //Outputs volatile bit gp0@GPIO.0;//analog i/p volatile bit gp1@GPIO.1;//analog i/p volatile bit gp2@GPIO.2;//motor drive volatile bit gp4@GPIO.4;//enable volatile bit gp5@GPIO.5;//motor drive #define motor_clockwise gp2 //pointer travels up #define motor_anticlockwise gp5 //pointer travels down #define enable gp4 unsigned char AD_Result; void a2d (char); void interrupt( void ) { //Handle timer0 interrupt if( intcon & (1<<T0IF) ) { clear_bit( intcon, T0IF ); //clear timer 0 interrupt bit } //Handle timer1 interrupt if( pir1 & (1<<TMR1IF) ) { clear_bit( pir1, TMR1IF ); //clear timer 1 interrupt bit } } void main( void ) { //Configure GPIO port trisio = 0x03;//gpio0 & gpio1 as a2d input. gpio = 0x00; cmcon = 0;//comparator off. //ansel = 0; //Set Timer0 mode clear_bit( option_reg, T0CS ); //configure timer0 as a timer //Set prescaler assignment clear_bit( option_reg, PSA ); //prescaler is assigned to timer0 //Set prescaler rate clear_bit( option_reg, PS2 ); //prescaler rate 1:2 clear_bit( option_reg, PS1 ); clear_bit( option_reg, PS0 ); //Set timer0 source edge selection set_bit( option_reg, T0SE ); //increment on high-to-low transition on RA4/T0CKI pin //Set timer 1 prescaler rate clear_bit( t1con, T1CKPS1 ); //prescaler rate 1:1 clear_bit( t1con, T1CKPS0 ); //Set timer 1 mode clear_bit( t1con, TMR1ON ); //disable timer 1 //Enable interrupts (Timer0) intcon = 10100000b; a2d(AD_Result);//pot i/p. //Endless loop while( 1 ) { enable = 1; if(AD_Result>5)//arbitory value to prove a2d returning sensible value. { motor_clockwise = 0; motor_anticlockwise = 1; motor_clockwise = 1; motor_anticlockwise = 0; } }//while }//main //////////////////////////////////////////////////////////////////////////////////////////// void a2d (char AD_Result) { clear_bit(ansel,ADCS2); set_bit(ansel,ADCS1); clear_bit(ansel,ADCS0); clear_bit(ansel,ANS3); clear_bit(ansel,ANS2); clear_bit(ansel,ANS1); set_bit(ansel,ANS0); set_bit(adcon0,ADFM); // Turn on the A/D converter, select channel 0. clear_bit(adcon0,VCFG); clear_bit(adcon0,CHS1); set_bit(adcon0,CHS0); set_bit(adcon0,ADON); set_bit(gpio,gp0); volatile bit Go_Done@ADCON0.1; volatile unsigned char ad_h@ADRESH; volatile unsigned char ad_l@ADRESL; unsigned int shiftH; unsigned int shiftL; set_bit(adcon0,Go_Done); // Start conversion delay_10us(2); shiftH = ad_h; shiftL = ad_l; AD_Result = (shiftH<<2) | (shiftL>>6); // Combine the two registers to get full 10 bits. adcon0 = 0; // Shut off the AD converter. };
  11. Ok, thanks. Will they be supported as I'm sure I will not be the only one with this headache? Over to you guys, Dave and Pavel! Regards, Daz.
  12. Hi all, I am trying to print letters sourced from an array onto a lcd. char array[2] = {'a','b'}; unsigned int i; for(i=0; i<2; i++) { lprintf("%C",array[i]); } Using "%C" or "%S" does not work. Regards, Daz.
  13. Hi all, I'm using the SPI feature on a 16f877. Does anyone know how I would go about accessing memory location 0x80 of a slave device and writing or reading the contents of sspbuf to or from it? I can read and write to the slave at the moment, but I need to read / write to specific memory locations. Regards, Daz.
  14. Thanks emte, At last it makes more sense, I will now re-evaluate and hopefully get the desired result. Regards, Daz.
  15. Obviously the 'oo' function code doesn't work as expected (proved by the lack of replies).
×
×
  • Create New...