News:

Let's find out together what makes a PIC Tick!

Main Menu

Value overflow

Started by midali, Mar 01, 2022, 12:59 PM

Previous topic - Next topic

midali

I have to read a serial data from FlySky transmitter and convert the data to pulsout for servo drive . In this code I have warning at line 41 and 44 : " Value will overflow a Byte size variable! "
Can somebody help me to fix the error  ?

Thank you !

Device = 16F1938

Config1 FCMEN_OFF, FOSC_INTOSC, WDTE_OFF, MCLRE_OFF, PWRTE_OFF, CP_ON, MCLRE_OFF, BOREN_OFF, CLKOUTEN_OFF, FCMEN_OFF
Config2 WRT_OFF, VCAPEN_OFF, PLLEN_OFF, STVREN_OFF, LVP_OFF

Declare  Xtal = 32
Declare  Adin_Res   = 10 '10 bit
Declare  Adin_Tad   = FRC 'RC oscillator chosen for the ADC
Declare  Adin_Stime = 50  'Allow 50us sample time

Declare Hserial_Baud = 115200
Declare Hserial_Clear = On

ADCON0 = %00000000   
ADCON1 = %10000000 ' left shift

'OSCCON = %01101000 'set to 4MHz
'OSCCON = %01111000 'set to 16MHz
OSCCON = %11110000 'set to 32MHz

TRISA  = %00000000

Dim ch1 As Word
Dim ch2 As Word
Dim ch3 As Word
Dim ch4 As Word
Dim ch5 As Word
Dim ch6 As Word
Dim ch7 As Word
Dim ch8 As Word
Dim ch9 As Word
Dim ch10 As Word             
ch1 = 100
ch2 = 66
                 
Dim timer1 As TMR1L.Word

begin:
     HRSIn Wait($20,$40),ch1,ch2,ch3,ch4,ch5,ch6,ch7,ch8,ch9,ch10
     While timer1<20000 :Wend   'wait until end of 20mS frame
     T1CON=100000        'Tmr1 Off   ===LINE41
     timer1=0            'reset Tmr1
     PORTA  = %00001111  'set A.0, A.1, A.2, A.3  outputs high
     T1CON=100001        'start timer  ====Line 44
     
chloop:
     If timer1>ch1 Then PORTA.0=0     'Approx 2uS/command
     If timer1>ch2 Then PORTA.1=0
     If timer1>ch3 Then PORTA.2=0
     If timer1>ch4 Then PORTA.3=0
     If timer1>10000 Then GoTo begin
GoTo chloop

John Lawton

Line 41 has incorrect syntax for a binary value, I think you meant to write
T1CON = %100000

Similarly line 44 should have a % preceding the value.

top204

#2
John is correct.

As can be seen by the clear warning message, the 8-bit T1CON SFR cannot be loaded with the values 100000 or 100001. An 8-bit variable can only hold a value between 0 to 255. The binary constant values are missing their preceding percentage characters "%"

keytapper

A small question.
What happen to those bit value without leading zeros? How the compile will take care of them?
For example %100 instead of %0000100.
I think that the longest format is less prone to errors.
Ignorance comes with a cost

midali

Thank you for help , it's clear , missing preceding percentage characters .
This code is from old forum,  Now, I haven't any errors when compile , but the code is not working .
So, I have to read a UART data and convert to servo output.
Any ideea is welcome!
Thank you again !!

tumbleweed

begin:
     HRSIn Wait($20,$40),ch1,ch2,ch3,ch4,ch5,ch6,ch7,ch8,ch9,ch10
     While timer1<20000 :Wend   'wait until end of 20mS frame

When the program starts up, TMR1 is not running so it will stay in the 'While timer1<20000' loop forever.

top204

#6
Remember keytapper, a binary value is converted from LSB (Least Significant Bit) first, so the Most Significant zeroes that are not in the text are automatically made zeroes by clearing the integer variable they are converted into before the conversion from "Binary String To Int" occurs.

midali

I verified with attention the code and is clear that it's wrong concept  . Thank you all for help !

david

Hi,
This is not optimized but should get you working -
It's reading 8 channels from the iBus output but only presenting 4 PWM channels.

 Device = 12LF1840
Config1 FCMEN_OFF, IESO_OFF, CLKOUTEN_OFF, BOREN_OFF, CPD_OFF, CP_OFF, MCLRE_OFF, PWRTE_ON, WDTE_OFF, FOSC_INTOSC
Config2 LVP_OFF, BORV_19, STVREN_OFF, PLLEN_ON, WRT_OFF   


Declare Xtal = 32 'Internal OSC
OSCCON = %11110000    '16MHz=01111010, 4MHz=01101010, 8MHz =01110010
APFCON = %10000100    'Rx=RA.5
TRISA = %00101000     '
ANSELA=0              '
WPUA=0                '
PORTA=0
T1CON=%00110000        '=Tmr1 OFF  %00110001=Tmr1 ON
All_Digital=true

'***************** Serial port declares ********************************
Declare Hserial_Baud=115200
Declare Hserial_SPBRG=34
RCSTA=%10010000
TXSTA=%00000000      'tx disabled
BAUDCON=%01001000   ' inverter
Declare Hserial_Clear=On

Dim Ch1 As Word
Dim Ch2 As Word
Dim Ch3 As Word
Dim Ch4 As Word
Dim Ch5 As Word
Dim Ch6 As Word
Dim Ch7 As Word
Dim Ch8 As Word

Dim Time As Word

begin:
     
     T1CON=%00110000      'Tmr1 Off
     TMR1H=0              'clear TMR1
     TMR1L=0
     HRSIn Wait($20,$40),Ch1,Ch2,Ch3,Ch4,Ch5,Ch6,Ch7,Ch8    ',Ch9,Ch10
     T1CON=%00110001     'start timer
     DelayUS 10          'fudge factor for loop delay.
     PORTA=23            'set 4 outputs high
         
chloop:
      Time=TMR1L+(TMR1H<<8)        'fix the time
      If Time>Ch1 Then PORTA.0=0   'compare channel
      If Time>Ch2 Then PORTA.1=0
      If Time>Ch3 Then PORTA.2=0
      If Time>Ch4 Then PORTA.4=0
      If TMR1H>42 Then GoTo begin    'Approx TMR1=11000
      GoTo chloop
           
End

Good luck.

David

david

Where did that smiley come from?   It should be replaced by numeral eight, close brackets.  Is this a quirk in the system?  Shall I try posting it again from my code?
Time=TMR1L+(TMR1H<<8)
Looks ok before posting but I'll give up if it goofs up again.

Yasin

Device = 12LF1840
Config1 FCMEN_OFF, IESO_OFF, CLKOUTEN_OFF, BOREN_OFF, CPD_OFF, CP_OFF, MCLRE_OFF, PWRTE_ON, WDTE_OFF, FOSC_INTOSC
Config2 LVP_OFF, BORV_19, STVREN_OFF, PLLEN_ON, WRT_OFF 


Declare Xtal = 32 'Internal OSC
OSCCON = %11110000    '16MHz=01111010, 4MHz=01101010, 8MHz =01110010
APFCON = %10000100    'Rx=RA.5
TRISA = %00101000    '
ANSELA=0              '
WPUA=0                '
PORTA=0
T1CON=%00110000        '=Tmr1 OFF  %00110001=Tmr1 ON
All_Digital=true

'***************** Serial port declares ********************************
Declare Hserial_Baud=115200
Declare Hserial_SPBRG=34
RCSTA=%10010000
TXSTA=%00000000      'tx disabled
BAUDCON=%01001000  ' inverter
Declare Hserial_Clear=On

Dim Ch1 As Word
Dim Ch2 As Word
Dim Ch3 As Word
Dim Ch4 As Word
Dim Ch5 As Word
Dim Ch6 As Word
Dim Ch7 As Word
Dim Ch8 As Word

Dim Time As Word

begin:
   
    T1CON=%00110000      'Tmr1 Off
    TMR1H=0              'clear TMR1
    TMR1L=0
    HRSIn Wait($20,$40),Ch1,Ch2,Ch3,Ch4,Ch5,Ch6,Ch7,Ch8    ',Ch9,Ch10
    T1CON=%00110001    'start timer
    DelayUS 10          'fudge factor for loop delay.
    PORTA=23            'set 4 outputs high
       
chloop:
      Time=TMR1L+(TMR1H<<8)        'fix the time
      If Time>Ch1 Then PORTA.0=0  'compare channel
      If Time>Ch2 Then PORTA.1=0
      If Time>Ch3 Then PORTA.2=0
      If Time>Ch4 Then PORTA.4=0
      If TMR1H>42 Then GoTo begin    'Approx TMR1=11000
      GoTo chloop
         
End

If you use it with a code tag it's fine.

John Drew

@david Because you didn't put your code in a code box the code contained a ": )" which is interpreted as a smiley. I put a space in the quote to avoid the problem with this post.
John

midali

The code is working, thank you very much David !!

It's something I can't understand:
QuotePORTA=%00011111
not put outputs high !!
The ports are high only if I define each port individually :
QuoteHigh PORTA.0 :High PORTA.1:High PORTA.2 : High PORTA.4




david

Quote from: John Drew on Mar 01, 2022, 10:22 PM@david Because you didn't put your code in a code box the code contained a ": )" which is interpreted as a smiley. I put a space in the quote to avoid the problem with this post.
John

Hmmm - I did wonder about that.  Normally I would use the code box but for shorter examples I don't bother.
Thanks.

David

david

Quote from: midali on Mar 01, 2022, 11:58 PMThe code is working, thank you very much David !!

It's something I can't understand:  not put outputs high !!
The ports are high only if I define each port individually : 

Hi,
You mean working with your device or with 12F1840?   
Not sure why your code shouldn't set the GPIO's high - perhaps have a look that they are properly configured for GPIO use and are not defaulting to comparators, ADC etc.
Good luck.

David



midali

I tested with 16F1938 . I thought maybe the ports have other configurations by default, but then it shouldn't go high if I configure each one separately.
This can mislead me, why doesn't the code work. In one day I'll verify with more attention from where error is .
Thank you again, David ,and all members !


top204

#16
You need to read a datasheet or two. :-)

The default, at power up, of the microcontroller's pins are as inputs. So placing the code PORTA.0 = 1 will not set the pin high because it is still an input! The TRISx registers alter a port's pin states to input or output, and the High and Low commands also alter the TRIS register associated with the port or pin chosen in the parameter. So... High PORTA.0, will alter TRISA.0 to make the pin an output, and set PORTA.0 to a 1, so the pin goes high.

Also, with devices that have an ADC, the pins are defaulted to analogue and not digital, but the compiler sets them to digital mode before the user's program starts. I have always thought this was a very silly mechanism, and pins should have defaulted to digital, which is the most used mode. But Microchip did this from the start, and we all know the logic (not) of Microchip!!

midali

Right now I finished to read in datasheet about TRISA  :) . I missed this opportunity and it made me think now when I saw an example of a functional code

I remember having this problem in the past, but I didn't pay attention to it because I met another uC with the same trouble . I have no experience, so I ignored it, I thought it was an error in my codes, anyway does't matter for me.

@ top204 Now everything is clear, I appreciate the effort and time lost to explain . Thank you very much !

david

#18
Hello midali,
Please be aware that while the compiler makes it very easy to decode the iBus string values with digital precision of 1uS, the actual time sensing loop (chloop in the attached section of code) will ultimately set the servo timing dither due to the loop time.  Imagine Ch1 has a value of 1500 and Time has a value of 1499 - the servo may not go low until the loop has gone around again which could be more than 1uS depending on the number of channels used and which ones need to be set low - hence some dither.  A fast response from the chloop is critical and if there's a better way to test the channel timing values it would be good to learn.   That said - if analog servos are used they will likely have a deadband of around 5uS anyway.

begin:
     
     T1CON=%00110000      'Tmr1 Off
     TMR1H=0              'clear TMR1
     TMR1L=0
     HRSIn Wait($20,$40),Ch1,Ch2,Ch3,Ch4,Ch5,Ch6,Ch7,Ch8    ',Ch9,Ch10
     T1CON=%00110001     'start timer
     DelayUS 10          'fudge factor for loop delay.
     PORTA=23            'set 4 outputs high
         
chloop:
      Time=TMR1L+(TMR1H<<8)        'fix the time
      If Time>Ch1 Then PORTA.0=0   'compare channel
      If Time>Ch2 Then PORTA.1=0
      If Time>Ch3 Then PORTA.2=0
      If Time>Ch4 Then PORTA.4=0
      If TMR1H>42 Then GoTo begin    'Approx TMR1=11000
      GoTo chloop



Cheers,
David

top204

You can also tighten the code up a bit by using:

Dim wTimer1 As TMR1L.Word  ' Combine TMR1L and TMR1H into a 16-bit SFR
Time = wTimer1             ' fix the time

 
Instead of:
 
Time=TMR1L+( TMR1H<<8 )        ' fix the time