Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Can't find code error

Status
Not open for further replies.

Monkeyman87

New Member
I have a really strange thing going on with my project. My project is an accelerometer that is interfaced with a PIC MCU which sends data to my PC. My problem is this: The PIC sends data values to the PC starting with decimal 255. From then on it sends a lesser value or greater value corresponding to the acceleration. Less is 255 and under and greater is 0 and greater. It is supposed to send a decimal value 127 corresponding to 0 g acceleration and send a greater or lesser value corresponding to greater or lesser acceleration since o g is 2.5v and the a/d is set to use vdd ref. This program worked great originally, then I modified a few things - changed the delay interval and added a collect/stop collect switch on gpio 2. I have included the code below and I would appreciate it if someone could tell me where I made a mistake. I have gone over the code again and again and I can't find the problem. Sorry, the code didn't wrap very well.

Code:
     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
     ;;	Accelerometer Project Code	;;
     ;;	By:		Sam Bixler				;;
     ;;	Date:	9-02-04					;;
     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	INCLUDE		"P12F675.INC"
	__CONFIG		0X3184
	cblock				0x20
	Xmit_Byte    	         ;holds byte to xmit
	Rcv_Byte     	         ;holds received byte 
	Bit_Cntr     		         ;bit counter for RS232
	Delay_Count  	         ;delay loop counter
	D1		  			   ;another delay loop counter
	D2					   ;another delay loop counter 			
	endc
                ORG		0X00
START	MOVLW	0X07		;TURN OFF COMPARATOR
	MOVWF	CMCON
	CLRF	GPIO						
	BSF	STATUS,	RP0	;BANK 1		
                MOVLW	B'00010010'				MOVWF	ANSEL	;ANALOG INPUT ON GPIO, 1 , SET FOSC
	MOVLW	B'11111110'		
	MOVWF	TRISIO	;GPIO, 0 IS OUTPUT FOR SERIAL TX 
	BCF 	STATUS,	RP0	;BANK 2
	MOVLW	B'00000101'
                MOVWF	ADCON0	;TURN ON A/D,  SELECT CHANNEL 1
	BSF	GPIO,	0	;MARK STATE TX

MAIN	BTFSS		GPIO,	2
;CHECK IF READY TO START DATA AQUISITION AND SEND
	GOTO		MAIN			
	BSF     ADCON0,	GO	;START CONVERSION	
ADTEST     BTFSS PIR1,	ADIF
	GOTO  ADTEST
	BCF     PIR1,	ADIF
	CALL		Delay			            MOVF   ADRESH,	W;MOVE THE MSBs OF A/D TO W
	CALL		XMIT_RS232	;TX DATA TO PC
	GOTO		MAIN		;DO IT AGAIN
													                                      
;Serial routines


XMIT_RS232  	MOVWF   Xmit_Byte            			;MOVE W TO Xmit_Byte
            		MOVLW   0x08                  			;SET 8 BITS OUT
            		MOVWF   Bit_Cntr
            		BCF     	GPIO, 0
            		CALL    	Bit_Delay
Ser_Loop   		RRF     	Xmit_Byte , f         			;SEND ONE BIT
            		BTFSS   	STATUS    , C
            		BCF     	GPIO, 0
            		BTFSC   	STATUS    , C
            		BSF     	GPIO, 0
            		CALL    	Bit_Delay
            		DECFSZ  	Bit_Cntr  , f         			;TEST IF ALL DONE
            		GOTO    	Ser_Loop
            		BSF     	GPIO, 0
            		CALL    	Bit_Delay
				CLRF		Xmit_Byte
            		RETURN

Start_Delay 	MOVLW   0x0C
            		MOVWF   Delay_Count
Start_Wait  	NOP
            		DECFSZ  	Delay_Count , f
            		GOTO    	Start_Wait
            		RETURN

Bit_Delay   	MOVLW   0x18
            		MOVWF   Delay_Count
Bit_Wait    		NOP
            		DECFSZ  	Delay_Count , f
            		GOTO    	Bit_Wait
            		RETURN	
														; Time Delay = 0.0500030s Osc = 4 MHz

Delay
				movlw	d'65'
				movwf	D1
				movlw	d'238'
				movwf	D2
loop			decfsz	D2, f
				goto	loop
				decfsz	D1, f
				goto	loop
				return

				
				
				
				RETLW	0X00	

				END
 
is your switch a logic one.? when set? if not that could be a problem.
& will the input float high ? when it is not set ? that could be another problem..
 
Is it just me?, or are you not setting ADCON1 anywhere?. As you're using it as an 8 bit A2D you need to clear the ADFM bit.

I would also suggest you convert the value to ASCII before you send it, in case of any binary problems with the PC.
 
Do you mean ADCON1 (there is no ADCON1 that I'm aware of ) or ADCON0 bit 1? I set the ADCON0 1(GO) bit right here
BSF ADCON0, GO ;START CONVERSION
ADTEST BTFSS PIR1, ADIF

Then the hardware clears it when the conversion is done.
I also clear the ADFM bit right here

MOVLW B'00000101'
MOVWF ADCON0 ;TURN ON A/D, SELECT CHANNEL 1

How is it transmitted to ASCII since all the data has to be transmitted as binary?

Yes, the switch is logic 1. It is tied to the gnd with a high value resistor and the switch pulls the line high.
 
Monkeyman87 said:
Do you mean ADCON1 (there is no ADCON1 that I'm aware of ) or ADCON0 bit 1? I set the ADCON0 1(GO) bit right here
Sorry - wrong PIC :lol:

In the 12F675 the ADFM bit is in ADCON0 instead.

How is it transmitted to ASCII since all the data has to be transmitted as binary?

Why does your data have to be transmitted as binary?, it's normal to convert it to ASCII and send it as normal text, so 255 would be sent as '2', '5', '5', CR, LF - five bytes instead of one, but no troublesome control characters to worry about. It would also make it simple to use the full 10 bit resolution of the A2D in the PIC. If you need it as a number, the PC program simply converts it back before using it.

My tutorials include code to do it, you would obviously have to modify it slightly for the 12F675 though - for one thing, changing the A2D configuration settings :lol:
 
Monkeyman87 said:
Could that be the source of the problem?

Possibly?, a serial port normally strips out certain ASCII control codes and performs certain functions, clear screen, LF, CR, things like that. I'm not sure what Windows actually does, but personally I wouldn't like to trust it! :lol:
 
This program worked great originally, then I modified a few things -
hold on , you said the program was working..?
if it worked transmitting binary before , it should work now.
how are you transmitting the data to the PC ?? i did not see serial mentioned anywhere.
 
I had succesfully transmitted the data and got a approx. 50 % error I'm assuming it was mostly induced by the wrong bandpass filter cap.

Code:
;Serial routines 


XMIT_RS232     MOVWF   Xmit_Byte                     ;MOVE W TO Xmit_Byte 
                  MOVLW   0x08                           ;SET 8 BITS OUT 
                  MOVWF   Bit_Cntr 
                  BCF        GPIO, 0 
                  CALL       Bit_Delay 
Ser_Loop         RRF        Xmit_Byte , f                  ;SEND ONE BIT 
                  BTFSS      STATUS    , C 
                  BCF        GPIO, 0 
                  BTFSC      STATUS    , C 
                  BSF        GPIO, 0 
                  CALL       Bit_Delay 
                  DECFSZ     Bit_Cntr  , f                  ;TEST IF ALL DONE 
                  GOTO       Ser_Loop 
                  BSF        GPIO, 0 
                  CALL       Bit_Delay 
            CLRF      Xmit_Byte 
                  RETURN 

Start_Delay    MOVLW   0x0C 
                  MOVWF   Delay_Count 
Start_Wait     NOP 
                  DECFSZ     Delay_Count , f 
                  GOTO       Start_Wait 
                  RETURN 

Bit_Delay      MOVLW   0x18 
                  MOVWF   Delay_Count 
Bit_Wait          NOP 
                  DECFSZ     Delay_Count , f 
                  GOTO       Bit_Wait 
                  RETURN
 
Ok I edited the code inserting code from the analog tutorial, and piclist.com. The program transmits the Thou, Hund, Tens, Ones and a newline character, BUT what it transmits is wrong. For a og reading it sends 'maa newline with the 'maa' part changing with the acceleration. The strange thing is that it simulates perfectly in PIC Simulator IDE. Below is a picture of the result. I can't figure this one out. Right now I feel like sebi's avatar. Sorry about the size of the picture. Its hard to see but the software usart shows 0238 and the analog pin input is 238 Thanks in advance for your help

:)
 

Attachments

  • screencapture.jpg
    screencapture.jpg
    62.6 KB · Views: 550
I am using Hyperterminal, but I am not sure which protocol to use. I have it set on autodetect right now. I really do not know alot about hypertermibnal so I'm going to search for some info.
 
Monkeyman87 said:
I am using Hyperterminal, but I am not sure which protocol to use. I have it set on autodetect right now. I really do not know alot about hypertermibnal so I'm going to search for some info.

Set Hyperterminal to the same as your PIC is generating, usually 8 data bits, no parity, and one stop bit.
 
Oh right :lol: I thought by protocol you meant the ANSI or TTY or MINITEL etc. settings. My serial settings are correct. I have isolated the problem more. It seems that when the PIC is supposed transmit a hex 30(decimal 0) it transmits a hex 60(character '). If it should be a hex 31(decimal 1) it transmits a hex 61(character a). I can't make sense of it. Since the program simulates perfectly and I get the same result no matter which serial port logging program I use( even a very basic one that just reads whatever is on the port) It must be the serial routines in my program, which I am going to replace with one that is known to work.

:?
 
Eureka!!!!!!!!!!! :D At last some sucess. I used a different serial routine by Tony Nixon. AND programed a different new chip, and it worked. It did not work with the original chip even when using the new serial routine. But the new chip works good!! I'm still getting stray characters like before. Could this be a result of using the internal oscillator. I did not set oscal as I accidentally erased the oscal data. (duh)
 
Monkeyman87 said:
Eureka!!!!!!!!!!! :D At last some sucess. I used a different serial routine by Tony Nixon. AND programed a different new chip, and it worked. It did not work with the original chip even when using the new serial routine. But the new chip works good!! I'm still getting stray characters like before. Could this be a result of using the internal oscillator. I did not set oscal as I accidentally erased the oscal data. (duh)

The internal oscillators work fine at 9600 baud, I use them all the time. However, using the internal oscillator at the wrong frequency is VERY likely to give problems! - you MUST restore the OSCAL value if you want to use the internal oscillator.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top