serial data ideas please


Closed Thread
Results 1 to 22 of 22
  1. #1

    Default serial data ideas please

    I'm monitoring two lots of serial data using a 8mhz 16F886

    The first lot 9600,8,N,1 is sent automatically by an external device and by using the USART and DT interrupt driven buffer I can capture that data and send it out of the USART again nicely. This must continue in the background and it works well.

    Now I have some other 10400,8,N,1 serial data on another line coming into a normal pin so I'm limited to the serin/serin2/debugin commands. This data only appears on the line in response to a command I send out.

    I can send the command using serout2 and a for/next loop to send the packet byte by byte disabling the interrupt whilst each byte is sent, and servicing the interrupt between bytes. That works fine and the main data is still OK.

    Now I have tried recieving the response packet using serin2 and a for next loop and disabling the interrupt response whilst each byte arrives. But as i service the interrupt inbetween bytes the data is arriving too fast for the serin2 to keep up and after about two bytes it is corrupted as you can see the bits getting further to the left.

    There isnt much time between the incoming bytes for this new data and if i don't service the background interrupt for the other data between bytes then the usart overflows. Hmm?

    I can't crank up the clock speed on the 16F886 as it running on the int clock on a pcb

    I could substitue a 18F2620 which i have tried but I haven't got the clock speed/usart settings or anything right with that :?
    It worked at 8mhz but not at 32mhz.
    I did set the osc define to 32.

    Just after general brilliant ideas.

    I did try debug in that was fractionally better but corrupt after three bytes instead of two.

  2. #2
    Join Date
    Jul 2003
    Location
    USA - Arizona
    Posts
    156


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    A few ideas perhaps...
    I could substitue a 18F2620 which i have tried but I haven't got the clock speed/usart settings or anything right with that :?
    It worked at 8mhz but not at 32MHz.
    I did set the osc define to 32.
    I believe you need to specify the PLL as active to get 32MHz from the 8MHz (which I think is what your trying to use).

    I'm guessing you have something like this:
    Code:
    Main Loop:
     loop doing SERIN stuff
    
    Interrupt Service:
     Service HW USART for receive data, and re-send
    Perhaps you can create a ring buffer, and every time the HW-UART interrupt fires save the the data. The main loop would then look like.
    Code:
    Main Loop:
     Disable HW-USART interrupt
     Check HW-UART TX buffer is empty
     If empty, send next ring buffer data through HW-UART (you will do this writing directly to the TXREG and not using HSEROUT)
     Do SERIN stuff
     Enable HW-USART interrupt (for receive only)
    Interrupt service would then look like:
    Code:
    Interrupt Service:
     Service HW USART for receive only (store in ring buffer)

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    use DT int for the 104K serial on HWusart TX/RX and do the 9K serial in basic ???

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    Thanks for the ideas.

    Amgen sadly the hardware is from an old project the 10.4K serial is fixed on normal pins and the 9.6k continious data is on the usart.

    Languer at the moment when the RX INT fires my routine gets the incomming byte and fires it straight out of the USART TX, it then checks in case a second byte has arrived (Two tend to arrive together) and repeats the above if it has. Then it returns and by that time has missed a byte on the other serial in data.

    My INT Handler looks like this.

    Code:
    '-------------------------------------
    ' INTERUPT HANDLER
    '-------------------------------------
            
    INT_Serin:                                      'Serial Interrupt routine
    
        IF OERR Then usart_error                 'Check for USART errors
        ptr_in = (ptr_in + 1)                       'Increment ptr_in pointer (0 to 48)
        IF ptr_in > (BufMax-1) Then ptr_in = 0             'Reset pointer if outside of buffer
        IF ptr_in = ptr_out Then Buffer_Error            'Check for buffer overrun
        HSerin [buffer[ptr_in]]                     'Read USART and store data in next empty location
        ptr_out = (ptr_out + 1)                         'Increment ptr_out pointer (0 to 48)                          
        IF ptr_out > (BufMax-1) Then ptr_out = 0              'Reset pointer if outside of buffer 
        bufchar = buffer[ptr_out]                       'Read buffer location
        HSEROUT [BufChar]                       'Send the character
        Toggle RedLed                               'Flash the Red Led
        IF RCIF Then INT_Serin                     'Check for another character while we're here
    
    @ INT_RETURN                                   ;Return to program

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    Just trying the 18F2620 chip at 8mhz it works as expected. Lags behind after a few characters etc. But otherwise fine.

    But at 32mhz with PLL enabled the usart seems to have gone from outputing data at 9600 to 2400 according to my logic analyser :?

    If anything i would have expected it to go to 38400 not the other way round. I'm obviously missing a gothca somewhere.

    Code:
    #CONFIG
     __CONFIG _CONFIG1H, _OSC_INTIO67_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
     __CONFIG _CONFIG2L, _PWRT_OFF_2L & _BOREN_OFF_2L
     __CONFIG _CONFIG2H, _WDT_OFF_2H
     __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_OFF_3H
     __CONFIG _CONFIG4L, _STVREN_OFF_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
    #ENDCONFIG
    
    DEFINE OSC 32            'Set PicBasic Pro processor speed to 32 Mhz   
    OSCCON = %01110110         'Internal 8 mhz Osc and stable 
    OSCTUNE= %01000000        'Pllen Frequency Multiplier Enabled for 8mhz Clock enables 32mhz Clock!!
    
    CMCON  = %00000111        'Comparators Off
    CVRCON = %00000000        'CvRef Powered Down 
    
    CCP1CON= %00001100        'CCP1 Module PWM Mode
    CCP2CON= %00000000        'CCP2 Module Disabled 
    
    HLVDCON= %00000000        'HLVCON Disabled
    
    T1CON  = %00110000        '$30 = Prescaler 1:8, TMR1 OFF
        
    TRISA = %00001011         'SET PORTA0, A1 & A3 AS INPUTS, REST AS OUTPUTS
    TRISB = %00000000         'SET PORTB AS OUTPUTS
    TRISC = %10010000         'SET PORTC AS OUTPUTS EXCEPT PORT C4 & C7 
        
    ADCON0 = %00000001        'SETUP ADC & ENABLE ADC MODULE on AN0
    ADCON1 = %00001110        'SETUP ADC SET REFV to VDD & VSS AN0 
    ADCON2 = %00100010        'SETUP ADC FOSC/32 LEFT JUSTIFY TAD 8
    Any ideas?

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    Just checked and a pause is taking 4 x longer than before so looks like pbpro has compensated but that the pic is still stuck at 8mhz :?

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    Hi,
    Do you have to echo the char from within the ISR? Perhaps you could set a flag indicating that there is data to send and then do the sending a out_ptr handling in the main routine. That way you'll spend less time in the ISR. Changing:
    Code:
    bufchar = buffer[ptr_out]               'Read buffer location
    HSEROUT [BufChar]                       'Send the character
    to
    Code:
    HSEROUT [buffer[ptr_Out]]
    Might also save a couple of cycles.

    However, I think the main culprit is the saving/restoring of the system variables when entering and exiting the ISR (which DT-INTS does for you). You should be able to run the 18F2620 at 32Mhz by enabling the PLL as languer says.

    Just thinking out loud.....

  8. #8


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    I think I'm stuck with enabling the 32mhz mode. PBPRO is aware but the pic is not running at 32mhz :?

  9. #9
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    Hi,
    Normally you need to enable the PLL by setting the correct CONFIG (which you now have set to INTIO67) but it seems as if, when using the internal oscillator, you enable it by setting PLLEN bit (OSCTUNE.6) - which apparently have done... So, right now I can't think of anything in particular but I'd take an extra read thru the Oscillator section of the datasheet and take a look if there indeed IS a CONFIG which seems reasonable for internal oscillator WITH PLL.

    /Henrik.

  10. #10
    Join Date
    Jul 2003
    Location
    USA - Arizona
    Posts
    156


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    The one thing I can see would help is not to do the transmission inside the interrupt loop. Not sure how PBP implements the HSEROUT (you'll need to look at the ASM list for that), but most compilers will check the buffer empty condition, load the USART, and (many times) wait until the buffers empty again before exiting the routine.

    The USART normally consists of two registers: TXREG and TSR. You load the TXREG with the information you want to send, if the TSR is empty then the TXREG is loaded into the TSR on the next Tcy; this is not an issue. But if the TSR is full (or currently sending data) then you must wait until it empties before the TXREG can load it and create the Buffer Empty condition. Because the USART is a serial device, you could potentially need to wait for 8-bits to be serially sent, before the TXREG is emptied. This could cause a bottleneck on the transactions.

    In your case, since you are receiving and sending at 9600bps on one side, and receiving at 10400bps on another; you could potentially hold the 10400bps, while waiting for the TXREG to clear. To work around this, you can load the TXREG directly (if it's empty), and if not, wait for the next go-around until it empties. That way you eliminate the potential of holding the 10400bps reception waiting for the 9600bps transmission. Hopefully this is a bit more clear.

    Some Pseudo code:

    Code:
    Main_Loop:
        PIE1.RCIE = FALSE '(disable EUSART Receive Interrupt Enable bit)
        IF PIR1.TXIF = TRUE THEN '(if TX buffer empty)
            nextout = nextout % buffer_size
            HSEROUT buffer[nextout]   
            nextout = nextout + 1
        ENDIF
        SERIN.... (perform SERIN routine, with timeout - this is important)
        PIE1.RCIE = TRUE '(enable EUSART Receive Interrupt Enable bit)
        GOTO Main_Loop
    Code:
    Interrupt Service:
        nextin = nextin % buffer_size
        HSERIN buffer[nextin]   
        nextin = nextin + 1

  11. #11


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    Thanks for that idea. I'll try it

    I'm still concerned that the pic is not running at 32mhz yet? My config must be wrong somewhere :?

  12. #12
    Join Date
    Apr 2007
    Location
    Pennsylvania, USA
    Posts
    158


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    I run an 18F2620 at 32MHZ with the internal OSC. Here is my config.

    Code:
    OSCCON=(percent)01110000               ' SET TO 8 MHZ internal oscillator
    OSCTUNE=(percent)11000000              ' TURN ON PLL X4 FREQ NOW 32MHZ
    replace (percent) with %
    Shawn

  13. #13


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    Shawn

    Can you post this part of you config as well please?

    Code:
    #CONFIG
     __CONFIG _CONFIG1H, _OSC_INTIO67_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
     __CONFIG _CONFIG2L, _PWRT_OFF_2L & _BOREN_OFF_2L
     __CONFIG _CONFIG2H, _WDT_OFF_2H
     __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_OFF_3H
     __CONFIG _CONFIG4L, _STVREN_OFF_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
    #ENDCONFIG

  14. #14
    Join Date
    Apr 2007
    Location
    Pennsylvania, USA
    Posts
    158


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    Sorry, I don't put my configs in the code like that. I set them with PICFLASH when I program my pics. What you have looks correct to me though.
    Shawn

  15. #15


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    Pic now seems to be running at 32mhz and able to keep up, so problem may be solved. Thanks for the ideas.

  16. #16
    Join Date
    Apr 2007
    Location
    Pennsylvania, USA
    Posts
    158


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    What did you end up changing?
    Shawn

  17. #17


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    Just the oscon and osctune to your values.

  18. #18


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    DT Interrupts?

    A problem has appeared and when the routine has been runing for a random amount of time the TX side of the usart starts sending garbage causing a crash in my external equipment. I have used a scope and logic probe on the input/output and the incomming data is correct.

    I'm running the 18F2620 at 32mhz and have removed all my extra code so the pic is now just running this interupt driven USART rxd & txd 9600,8,E,1

    Any stupid errors spring to mind?

    Bizzarely when the code is running correctly if I touch a piece of wire to the GND on my pcb the usart crashes and sends garbage or if i connect my unconnected to anything else logic analyser to just the gnd pin the usart crashes? The wire or equipment does not have to be connected to anything so perhaps is picking up some EMI but how can that be affecting the pic? Any ideas on extra capacitance to add. Is the pic flaky at 32mhz? The pic and code seems solidly reliable otherwise.

    Code:
    '------------------------------ General 18F2620 configuration --------------------------
    
    #CONFIG
     __CONFIG _CONFIG1H, _OSC_INTIO67_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
     __CONFIG _CONFIG2L, _PWRT_OFF_2L & _BOREN_OFF_2L
     __CONFIG _CONFIG2H, _WDT_OFF_2H
     __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_OFF_3H
     __CONFIG _CONFIG4L, _STVREN_OFF_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
    #ENDCONFIG
    
    DEFINE OSC 32			'Set PicBasic Pro processor speed to 8 Mhz (Must change this if run at 32mhz mode)  
    OSCCON = %01110000 		'Internal 8 mhz Osc and stable 
    OSCTUNE= %11000000		'Pllen Frequency Multiplier Enabled for 8mhz Clock enables 32mhz Clock!!
    
    CMCON  = %00000111		'Comparators Off
    CVRCON = %00000000		'CvRef Powered Down 
    
    CCP1CON= %00001100		'CCP1 Module PWM Mode
    CCP2CON= %00000000		'CCP2 Module Disabled 
    
    HLVDCON= %00000000		'HLVCON Disabled
    
    T1CON  = %00110000		'$30 = Prescaler 1:8, TMR1 OFF
    	
    TRISA = %00001011 		'SET PORTA0, A1 & A3 AS INPUTS, REST AS OUTPUTS
    TRISB = %00000000 		'SET PORTB AS OUTPUTS
    TRISC = %11010000 		'SET PORTC AS OUTPUTS EXCEPT PORT C4 & C6,C7 
    	
    ADCON0 = %00000001		'SETUP ADC & ENABLE ADC MODULE on AN0
    ADCON1 = %00001110		'SETUP ADC SET REFV to VDD & VSS AN0 
    ADCON2 = %00100010		'SETUP ADC FOSC/32 LEFT JUSTIFY TAD 8
    
    DEFINE LCD_DREG PORTB 		'PORTB is LCD data port
    DEFINE LCD_DBIT 0 		'PORTB.0 is the data LSB
    DEFINE LCD_RSREG PORTC 		'RS is connected to PORTC.0
    DEFINE LCD_RSBIT 0
    DEFINE LCD_EREG PORTC 		'EN is connected to PORTC.1
    DEFINE LCD_EBIT 1
    DEFINE LCD_BITS 8 		'8 lines of data are used
    DEFINE LCD_LINES 4 		'It is a 4-line display
    DEFINE LCD_COMMANDUS 2000 	'Use 2000uS command delay
    DEFINE LCD_DATAUS 50 		'Use 50uS data delay
    
    DEFINE ADC_BITS 8      		'Set number of bits in result
    DEFINE ADC_CLOCK 3      	'Set Clock source (3 = rc)
    DEFINE ADC_SAMPLEUS 50     	'Set sampling time in uS
       
    DEFINE CCP1_REG PORTC 		'HPWM channel 1 pin Port C Backlight PWM Driver
    DEFINE CCP1_BIT 2 		'HPWM channel 1 pin Bit 2
    
    'Setup the hardware USART for 9600 baud
    
    DEFINE HSER_BAUD 9600		'Set Baud rate to 9600bps
    DEFINE HSER_BITS 9		'Set to 9 bit mode
    DEFINE HSER_EVEN 1		'Set Even Parity
    DEFINE HSER_CLROERR 1 		'Clear overflow error automatically
    
    
    'Button		var PORTA.0	'Joy Button Input
    HLineIn		var PORTA.1	'H Line In
    HLineOut	var PORTA.2	'H Line Out
    KLineIn		var PORTA.3	'K Line In
    KLineOut	var PORTA.4	'K Line Out
    GreenLed	var PORTA.5	'Green Led Out
    Scs		    var PORTA.6	'SCS Line Out
    Piezo		var PORTA.7	'Piezo Buzzer Out
    
    Lcd0		var PORTB.0	'Parallel Lcd0
    Lcd1		var PORTB.1	'Parallel Lcd1
    Lcd2		var PORTB.2	'Parallel Lcd2
    Lcd3		var PORTB.3	'Parallel Lcd3
    Lcd4		var PORTB.4	'Parallel Lcd4
    Lcd5		var PORTB.5	'Parallel Lcd5
    Lcd6		var PORTB.6	'Parallel Lcd6
    Lcd7		var PORTB.7 	'Parallel Lcd7
    
    LcdRS		var PORTC.0	'Parallel LcdRS Line
    LcdEN		var PORTC.1	'Parallel LcdEN Line 
    LcdLight	var PORTC.2	'Parallel Lcd Backlight Control
    I2CClock	var PORTC.3	'I2CClock Line
    I2CData		var PORTC.4	'I2CData Line
    RedLed		var PORTC.5	'Red Led Out
    'Txd		var PORTC.6	'Usart Logger Txd
    'Rxd		var PORTC.7	'Usart Logger Rxd
    
    
    '*******************************************************************************
    
    'Variables Constants and I/O definitions
    
    '------------------------ Variables 16bit --------------------------------------
    
    
    	
    '------------------- Variables 8bit --------------------------------------------
    
    IChkSum   var BYTE  	'MotA Line rolling Checksum Byte
    AssistLevel   var byte 	'Shows level of Ima Assist
    RegenLevel    var byte	'Shows level of Ima Regen
    FlagBits  var BYTE	'Flag Bits Byte (0-7)  
    DutyCycle var BYTE	'PWM DutyCycle for Lcd backlight [127 = 50% duty] 
    Key 	  var BYTE 	'Returned Button Value (0-5) 5 = No key pressed
    Gen	  var BYTE	'Gen is a General Use Byte variable (0-255)
    
    ImaCtrl	   var BYTE	'Ima Control Byte 0-8 (0-3 = Regen) 4 = Off (5-8 = Assist) 
    
    
    LCM	  VAR BYTE    	'Lcd Command Code 254
    
    LCL       VAR BYTE    	'Lcd Control Code  Clear Lcd
    
    L12       VAR BYTE    	'Lcd Control Code  Line 1 Column 0
    L22       VAR BYTE    	'Lcd Control Code  Line 2 Column 0
    L32       VAR BYTE    	'Lcd Control Code  Line 3 Column 0
    L42       VAR BYTE    	'Lcd Control Code  Line 4 Column 0
    
    L52       VAR BYTE    	'Lcd Control Code  Line 1 Column 10
    L62       VAR BYTE    	'Lcd Control Code  Line 2 Column 10
    L72       VAR BYTE    	'Lcd Control Code  Line 3 Column 10
    L82       VAR BYTE    	'Lcd Control Code  Line 4 Column 10
    
    '------------------- Variables 1bit --------------------------------------------
    
    
    AssistBit var FlagBits.Bit1 	'Assist Bit Shows when Ima Assist in operation. 
    RegenBit  var FlagBits.Bit2 	'Regen Bit Show when Ima Regen in operation. 
    
    
    
    '------------------------------ Main Program -----------------------------------
    
    EEPROM 1014, [254,1,128,192,148,212,138,202,158,222]	'20x4 Lcd Control Codes
    
    '**************************************************************************************************************	
    
    'Alias & Vars
    RCIF VAR PIR1.5             	'Receive  interrupt flag (1=full , 0=empty)
    TXIF VAR PIR1.4             	'Transmit interrupt flag (1=empty, 0=full)
    OERR VAR RCSTA.1            	'Alias OERR (USART Overrun Error Flag)
    CREN VAR RCSTA.4           	'Alias CREN (USART Continuous Receive Enable)
    
    Ptr_in VAR BYTE             	'Pointer - next empty location in buffer
    ErrFlag VAR BYTE            	'Holds error flags
    ptr_in = 0                  	'Clear buffer pointer (in)
    
    Errflag = 0                 	'Clear error flag
    Char var byte			'Define Char as the data byte being sent
    RCSTA.7 = 1             'Configue PortC 6 & 7 as Usart
    BAUDCON.4 = 1                   'Invert USART Output for use with Transistor driver  TXCKP
    
    '-----------------------------------------------------------------------------
    INCLUDE "DT_INTS-18.bas"
    INCLUDE "ReEnterPBP-18.bas"     	'Include if using PBP interrupts
    
    ASM
    INT_LIST  macro    ;IntSource,        Label,       Type,    ResetFlag?
            INT_Handler    RX_INT,      _INT_Serin,     PBP,        no
        endm
        INT_CREATE               	;Creates the interrupt processor
    ENDASM
    
    @   INT_ENABLE   RX_INT     	;Enable external (INT) interrupts
    '-----------------------------------------------------------------------------
    
    '***********************************************************************  
    
    'Skip over the interupt handler & subroutines
    
    	goto Start
    
    '***********************************************************************  
    
    '-------------------------------------
    ' INTERUPT HANDLER
    '-------------------------------------
    		
    INT_Serin:                  				'Serial Interrupt routine
    
    	IF OERR Then usart_error 			'Check for USART errors
    	HSerin [Char]     				'Read USART 
    	
    	if Char = $B3 then             			'Start of 9 byte packet
    	Ptr_In = 0                     			'Reset Ptr_In to 0
    	IChkSum = 0                    			'Clear Checksum Byte
    	else
    	Ptr_In = Ptr_In + 1            			'Increment pointer 
        	endif 
        
        	If Ptr_In = 1 then                 		'If Byte is 1 then check if assist or regen requested
           	If AssistBit = 1 then Char = %00010000        	'Modify byte 1 for Ima Assist Control = 16 Normal 
    	    if RegenBit  = 1 then Char = %00100000          'Modify byte 1 for Ima Regen  Control = 32 Normal
            endif
        	    
        	If Ptr_In = 2 then                 		'If Byte is 2 then check if assist or regen requested             
            if AssistBit = 1 then Char = AssistLevel   	'Load Char byte 2 with Ima Assist Value 0-127 = 0-100%
            if RegenBit  = 1 then Char = RegenLevel		'Load Char byte 2 with Ima Regen  Value 127-0 = 0-100%
        	endif 
            
            If Ptr_In = 5 then                 		'If Byte is 5 then check if Regen requested
            If AssistBit = 1 then Char = %00100001        	'Modify byte 5 for Ima Assist Control = 33
            if RegenBit  = 1 then Char = %00000001          'Modify byte 5 for Ima Regen Control  = 1
            endif
                
        	if Ptr_In = 8 then              		'Calculate IChkSum Byte
    	IChkSum = ~ IChkSum				'NOT CheckSum
    	IChkSum = IChkSum + 1				'Add 1 to CheckSum
    	Char = IChkSum & $7F	 			'AND $7F
        	else
       	IChkSum = IChkSum + Char       			'Add Char to accumulated data
        	endif     
        
        	hserout [Char]                              	'Sends data     
            	
    	IF RCIF = 1 Then INT_Serin 				'Check for another character while we're here
    	
    	Toggle RedLed  	                         	'Flash the Red Led
    
    
    @ INT_RETURN           					'Return to program
    
    USART_Error:                				'Error - USART has overrun (data too fast?)
    
        	errflag.0 = 1               			'Set the error flag for hardware 
    	CREN = 0 					'Disable continuous receive to clear overrun flag
    	CREN = 1            				'Enable continuous receive
    
    @ INT_RETURN           					'Return to program
    
    
    '**************************************************************************************************************	
    
    'OBDII Gauge Main Program  
    
    Start:                          			'Start of Program 
    	
    	ImaCtrl = 4					'Ima Control Byte = 4 (No Assist or Regen)
    	Gosub SetIma					'Gosub Set Ima Routine
    	
    		
    	Read 1014,LCM,LCL,L12,L22,L32,L42,L52,L62,L72,L82	'Load 20x4 Lcd Control Codes from EEprom adr 
    
    	HPWM 1, 255, 500   				'Change/Start 500hz HPWM 'Set Backlight Duty 0 = Off 255 = FullOn
    
       	Pause 2000                              	'Pause for 2.0 seconds
    
    	LCDOUT LCM,LCL 
    	
    	    	
    '**************************************************************************************************************	
    
    DispLoop:						'Main Loop goes here
    
    	Toggle GreenLed					'Flashes Green Led
      	
    '***************************** Key Functions KeyBit 0 ****************************************
    
    '*Key Check*
    
    	Gosub KeyPress					'Gosub KeyPress Routine 5 = No Key Pressed
    	
    '*Key Bit Toggle* Press
    
        if Key = 0 then					'If Key = 0 (Press) then 
    	ImaCtrl = 4					'Ima Control Byte = 4 (No Assist or Regen)
    	Gosub SetIma					'Gosub Set Ima Routine
    	endif      
    
            
    '*Assist Up / Regen Down* Button Right
    
    	if Key = 2 then			'If Key = 2 (Right) and KeyBit = 0 then (Toggle Assist Bit)
    	If ImaCtrl < 8 then ImaCtrl = ImaCtrl + 1	'Increment ImaCtrl Byte if < 8
    	Gosub SetIma					'Gosub Set Ima Routine
    	endif 
    
    '* Assist Down / Regen Up* Button Left
    	
    	if Key = 4 then			'If Key = 4 (Left) and KeyBit = 0 then (Toggle Regen Bit)
    	If ImaCtrl > 0 then ImaCtrl = ImaCtrl - 1	'Decrement ImaCtrl Byte if > 0
    	Gosub SetIma					'Gosub Set Ima Routine
        	endif  
    
          	
    '* Display Assist/Regen Status on Screen
    	
    	LCDOUT LCM,146					'Set Display to Line 1, Column 19        	
    
        	if AssistBit = 1 then LCDOUT "A",#4 - (8-ImaCtrl)	'Display Assist Data on screen if active
        	if RegenBit  = 1 then LCDOUT "R",#4 - ImaCtrl 	     	'Display Regen  Data on screen if active
    	if AssistBit = 0 and RegenBit = 0 then LCDOUT 32,32	'Blank Assist/Regen data if not active    
    
        pause 250
    
    	goto DispLoop					'Goto Main Loop 
    
    
    '****************************** Set Ima Routine *******************************
    
    SetIma:							'Set Ima Routine 4 levels of assist/regen 25,50,75,100%
    
        	If ImaCtrl = 0 then				'If ImaCtrl = 0 then set Regen 100%
    	RegenLevel = 0           			'Set Regen to 100%
    	RegenBit = 1					'Set Regen Bit (Regen On)
    	elseif ImaCtrl = 1 then				'If ImaCtrl = 1 then set Regen 75%
    	RegenLevel = 64           			'Set Regen to 75%
    	RegenBit = 1					'Set Regen Bit (Regen On)
    	elseif ImaCtrl = 2 then				'If ImaCtrl = 2 then set Regen 50%
    	RegenLevel = 96           			'Set Regen to 50%
    	RegenBit = 1					'Set Regen Bit (Regen On)
    	elseif ImaCtrl = 3 then				'If ImaCtrl = 3 then set Regen 25%
    	RegenLevel = 116           			'Set Regen to 25%
    	RegenBit = 1					'Set Regen Bit (Regen On)
    
    	elseif ImaCtrl = 4 then				'If ImaCtrl = 4 then set Assist & Regen to 0%
    	RegenLevel = 0           			'Set Regen to 0%
    	RegenBit = 0					'Clear Regen Bit (Regen Off)
    	AssistLevel = 0           			'Set Assist to 0%
    	AssistBit = 0					'Clear Assist Bit (Assist Off)
    
    	elseif ImaCtrl = 5 then				'If ImaCtrl = 5 then set Assist 25%
    	AssistLevel = 15           			'Set Assist to 25%
    	AssistBit = 1					'Set Assist Bit (Assist On)
    	elseif ImaCtrl = 6 then				'If ImaCtrl = 6 then set Assist 50%
    	AssistLevel = 35           			'Set Assist to 50%
    	AssistBit = 1					'Set Assist Bit (Assist On)
    	elseif ImaCtrl = 7 then				'If ImaCtrl = 7 then set Assist 75%
    	AssistLevel = 76           			'Set Assist to 75%
    	AssistBit = 1					'Set Assist Bit (Assist On)
    	elseif ImaCtrl = 8 then				'If ImaCtrl = 8 then set Assist 100%
    	AssistLevel = 126           			'Set Assist to 126
    	AssistBit = 1					'Set Assist Bit (Assist On)
    	endif
    
        	return						'Return to Main Program 	
    
    
    '****************************** Buttons Routine *******************************
    
    'Check Button is pressed > Push = 0 Up = 1 Right = 2 Down = 3 Left = 4 No Key = 5 (Count1 = Press Duration)
    
    KeyPress:                    				'Decodes Joybutton Input and returns 5 values (0,1,2,3,4) in Key 
        	
        	ADCIN 0, Gen					'Get value 0-255
    	key = 5                         		'Set Default value for Key
    	If Gen > 234 then return			'If No key pressed then key = 5 return
    	if Gen < 235 then key = 2			'Test if button 2 pressed Right
    	if Gen < 192 then key = 1			'Test if button 1 pressed Up
    	if Gen < 150 then key = 4			'Test if button 4 pressed Left
    	if Gen < 107 then key = 3			'Test if button 3 pressed Down
    	if Gen < 64  then key = 0			'Test if button 0 pressed Press	
    	
    	High Piezo                             		'Sound Piezo
      	pause 75                              		'Pause for 0.075 seconds
    	low Piezo                              		'Stop Piezo
    	return						'Return to main program

  19. #19
    Join Date
    Jul 2003
    Location
    USA - Arizona
    Posts
    156


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    Sounds like there may be a HW issue. What does the simplified code looks like? Have you checked the errata on the device? There are USART flags for every silicon - but more so for the rev A's.

  20. #20


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    How do you know which version of silicon you have?

    Does PBP 3.04 code take into account these below errata issues?

    This looks suspect as I am using 9 bit HSEROUT mode with parity etc.


    17. Module: EUSART
    When performing back-to-back transmission in
    9-bit mode (TX9D bit in the TXSTA register is
    set), an ongoing transmission’s timing can be
    corrupted if the TX9D bit (for the next
    transmission) is not written immediately following
    the setting of TXIF. This is because any
    write to the TXSTA register results in a reset of
    the baud rate timer which will effect any ongoing
    transmission.
    Work around
    Load TX9D just after TXIF is set, either by polling
    TXIF or by writing TX9D at the beginning of the
    Interrupt Service Routine, or only write to TX9D
    when a transmission is not in progress
    (TRMT = 1).
    Date Codes that pertain to this issue:
    All engineering and production devices.

    18. Module: EUSART
    When performing back-to-back transmission in 9-bit
    mode (TX9D bit in the TXSTA register is set), the
    second byte may be corrupted if it is written into
    TXREG immediately after the TMRT bit is set.
    Work around
    Execute a software delay, at least one half the
    transmission’s bit time, after TMRT is set and prior
    to writing subsequent bytes into TXREG.
    Date Codes that pertain to this issue:
    All engineering and production devices.
    4. Module: Enhanced Universal
    Synchronous Receiver
    Transmitter (EUSART)
    One bit has been added to the BAUDCON register
    and one bit has been renamed. The added bit is
    RXDTP and is in the location, BAUDCON<5>. The
    renamed bit is the TXCKP bit (BAUDCON<4>),
    which had been named SCKP.
    The TXCKP (BAUDCON<4>) and RXDTP
    (BAUDCON<5>) bits enable the Asynchronous
    mode TX and RX signals to be inverted (polarity
    reversed). RXDTP has no effect on the
    Synchronous mode DT signal.
    Register 18-3, on page 204, will be changed as
    shown on page 3.
    Work around
    None required.
    Date Codes that pertain to this issue:
    All engineering and production devices
    :?

  21. #21
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    I doubt it is the PIC.

    With all the stuff you have going on in the ISR and the possible jumping out of the ISR is one problem. ISRs should be short and it is not a good idea to jump out of one.

    Then you describe the touching of a wire to the board causes problems... That is a hardware problem you have, not the PIC. Post a schematic and someone may be able to spot the problem there.
    Dave
    Always wear safety glasses while programming.

  22. #22
    Join Date
    Jul 2003
    Location
    USA - Arizona
    Posts
    156


    Did you find this post helpful? Yes | No

    Default Re: serial data ideas please

    The MCU is HW; so yes it could have an issue. That being said, there can also be an issue with the supporting HW (i.e. around the MCU itself).

    As for reading the silicon revision; look for DEVID1 on datasheet.
    PicKit can read it. From Readme File of PicKit2: NOTE: silicon revision display is enabled by adding the INI file value "REVS: Y".
    This link should also give you ideas on how to read it from the PIC itself:http://www.microchip.com/forums/m34318.aspx.

    You say you have code which only does the UART stuff and still has the issue. What does that code looks like? I presume it shouldn't have any of the fancy interrupt, LCD, or other stuff.
    Last edited by languer; - 2nd July 2012 at 07:56.

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts