RE0 analog stopped working when receive string 18f4550

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
NAGARSU5
Posts: 12
Joined: 19 Nov 2016 05:06

RE0 analog stopped working when receive string 18f4550

#1 Post by NAGARSU5 » 19 Nov 2016 05:36

Hi,

my code is working fine. But only when start receiving the string via UART (RC7), the RE0 (AN5) just get hanged, and the analog value doesn't move on the LCD.So anything need to check on the configuration bits? FYI im using 18F4550. Thanks.
Attachments
2016-11-19_12-10-32.jpg
2016-11-19_12-10-32.jpg (210.76 KiB) Viewed 2540 times

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: RE0 analog stopped working when receive string 18f4550

#2 Post by Aleksandar.Mitrovic » 21 Nov 2016 15:50

Hi,

Are you using some interrupts for UART?

Can you share your code with us?

Kind regards,
Aleksandar

NAGARSU5
Posts: 12
Joined: 19 Nov 2016 05:06

Re: RE0 analog stopped working when receive string 18f4550

#3 Post by NAGARSU5 » 23 Nov 2016 02:46

Here the codes. Well this is my first project involving of microcontroller. Correct me if i'm wrong. Thanks



program MyProject
' Declarations section
dim SW1 as sbit at PORTB.2' Trip value Decrease switch (S1)
dim SW2A as sbit at PORTC.4' Digital/ Analog select
dim SW2B as sbit at PORTC.5' On-board switches Disable/ Enable select
dim SW2C as sbit at PORTB.3' Trip Relay Momentary/ Latch select
dim SW3 as sbit at PORTC.0' Trip value Increase switch (S3)
dim SW4 as sbit at PORTC.3' Run Hours value Reset switch
dim I0 as sbit at PORTA.1' Digital input I0
dim I1 as sbit at PORTA.0' Digital input I1
dim I2 as sbit at PORTA.5' Digital input I2
dim I3 as sbit at PORTA.4' Digital input I3
dim I4 as sbit at PORTA.3' Digital input I4
dim I5 as sbit at PORTA.2' Digital input I5
dim L3 as sbit at PORTD.6'
dim L1 as sbit at PORTD.7'
dim OUT_K1 as sbit at PORTE.2'
dim Q0 as sbit at PORTB.0 '
dim Q1 as sbit at PORTB.1 '
dim Q2 as sbit at PORTC.2 '
dim Q3 as sbit at PORTC.1 '
dim M53 as bit'
dim M55 as bit'
dim M100 as bit'
dim M101 as bit'
dim M102 as bit'
dim M103 as bit'
dim M104 as bit'
dim M105 as bit'
dim M106 as bit' for page selection use
dim M107 as bit' for EEPROM use
dim M108 as bit' FOR EEPROM
dim M109 as bit' FOR EEPROM
dim M110 as bit' FOR EEPROM
dim M0 as bit'
dim M1 as bit'
dim M2 as bit'
dim M3 as bit'
dim M4 as bit'
dim M5 as bit'
dim M6 as bit'
dim T0 as byte'
dim T1 as byte'
dim T2 as byte'
dim T3 as byte'
dim D0 as float' LOW
dim D1 as float' HIGH
dim D7 as word' for HourMeter use
dim D21 as word' ACTUAL
dim D22 as byte' PI Trimpot
dim D23 as word' millivolt AI value
dim D24 as float' Max.Time capture
dim D25 as byte' For Bargraph use
dim C0 as byte'
dim C1 as word'
dim C2 as word'
dim C16 as word'
dim temp as byte'
dim D100 as float' Actual value in float
dim D200 as float'
dim Dset as float'
dim DCAL as float'
dim msg1 as string[16] 'string buffer to display numerical value
const STRING_MAX_SIZE =15
dim ReceiveBuffer as string[STRING_MAX_SIZE+1]
dim TerminationString as string[3]
dim StringA as string[5]'
dim StringB as string[5]'
dim StringC as string[5]'
dim Unit as char
'
'
'
'constants declaration
const dsp1 =":"
const dsp2="Max." 'Maximum captured
const dsp3="OK"
const dsp4="LOW!"
const dsp5=" "
const dsp6=">"
const dsp7="L:"
const dsp8="H:"
const dsp9="<"
const dsp10="AI=mV:"
const dsp11="s"
const dsp12="Q0-3:"
const dsp13="0"
const dsp14="1"
const dsp15="T"
const dsp16="I0-5:"
const dsp17="HMeter:"
const dsp18="CURRENT RELAY"
const dsp19="SCR-X10V V2.1.0" ' Firmware version
'
'********************************************************************
sub procedure LCD_Firstpage
'LCD Print_out (LOW, HIGH, ACTUAL)
msg1=dsp7 ' MIN
LCD_Out(1,1,msg1)
msg1=dsp8
LCD_Out(1,10,msg1)
floattostr(D0,msg1)
LCD_Chr(1,3,msg1[0])
LCD_Chr(1,4,msg1[1])
LCD_Chr(1,5,msg1[2])
LCD_Chr(1,6,msg1[3])
LCD_Chr(1,7,dsp5)
LCD_Chr(1,8,dsp5)
LCD_Chr(1,9,dsp5)
'
floattostr(D1,msg1)
LCD_Chr(1,12,msg1[0])
LCD_Chr(1,13,msg1[1])
LCD_Chr(1,14,msg1[2])
LCD_Chr(1,15,msg1[3])
'
floattostr(D100,msg1) ' float to string
LCD_Chr(2,1,msg1[0]) '
LCD_Chr(2,2,msg1[1]) '
LCD_Chr(2,3,msg1[2]) '
LCD_Chr(2,4,msg1[3]) '
LCD_Chr(2,5,Unit) ' print char.UNIT
end sub
'*********************************************************************
'*********************************************************************
'*********************************************************************
'*********************************************************************
'*********************************************************************
sub procedure Low_High_Unit_Set
'
if M108 then ' Write registered value into EEPROM for LOW
temp=Lo(D0)
EEPROM_Write($16,temp)
temp=Hi(D0)
Delay_ms(50)
EEPROM_Write($17,temp)
temp=Higher(D0)
Delay_ms(50)
EEPROM_Write($18,temp)
temp=Highest(D0)
Delay_ms(50)
EEPROM_Write($19,temp)
M108=0
end if
'
if M109 then ' Write registered value into EEPROM for HIGH
temp=Lo(D1)
EEPROM_Write($20,temp)
temp=Hi(D1)
Delay_ms(50)
EEPROM_Write($21,temp)
temp=Higher(D1)
Delay_ms(50)
EEPROM_Write($22,temp)
temp=Highest(D1)
Delay_ms(50)
EEPROM_Write($23,temp)
M109=0
end if
'
if M110 then ' Write registered value into EEPROM for UNIT
temp=Lo(Unit)
EEPROM_Write($24,temp)
M110=1
end if
end sub
'*********************************************************************
const character as byte[8] = (31,0,0,0,0,0,0,31)
sub procedure CustomChar(dim pos_row as byte, dim pos_char as byte)
dim i as byte
Lcd_Cmd(64)
for i = 0 to 7
Lcd_Chr_CP(character)
next i
Lcd_Cmd(_LCD_RETURN_HOME)
Lcd_Chr(pos_row, pos_char, 0)
end sub
'*********************************************************************
const character2 as byte[8] = (31,31,31,31,31,31,31,31)
sub procedure CustomChar2(dim pos_row as byte, dim pos_char as byte)
dim i as byte
Lcd_Cmd(72)
for i = 0 to 7
Lcd_Chr_CP(character2)
next i
Lcd_Cmd(_LCD_RETURN_HOME)
Lcd_Chr(pos_row, pos_char, 1)
end sub
'*********************************************************************
const character3 as byte[8] = (28,4,6,6,6,6,4,28) ' bargraph cap
sub procedure CustomChar3(dim pos_row as byte, dim pos_char as byte)
dim i as byte
Lcd_Cmd(80)
for i = 0 to 7
Lcd_Chr_CP(character3)
next i
Lcd_Cmd(_LCD_RETURN_HOME)
Lcd_Chr(pos_row, pos_char, 2)
end sub
'*********************************************************************
const character4 as byte[8] = (31,21,4,4,4,21,14,4)
sub procedure CustomChar4(dim pos_row as byte, dim pos_char as byte)
dim i as byte
Lcd_Cmd(88)
for i = 0 to 7
Lcd_Chr_CP(character4)
next i
Lcd_Cmd(_LCD_RETURN_HOME)
Lcd_Chr(pos_row, pos_char, 3)
end sub
'********************************************************************
sub procedure Max_value_page
msg1=dsp2
LCD_Out(1,1,msg1)
LCD_Chr(1,5,Unit)
LCD_Chr(1,6,dsp1)
floattostr(D24,msg1)
LCD_Chr(1,8,msg1[0])
LCD_Chr(1,9,msg1[1])
LCD_Chr(1,10,msg1[2])
LCD_Chr(1,11,msg1[3])
LCD_Chr(1,12,dsp5)
LCD_Chr(1,13,dsp5)
LCD_Chr(1,14,dsp5)
LCD_Chr(1,15,dsp5)
LCD_Chr(1,16,dsp5)
'
floattostr(D100,msg1) ' float to string
LCD_Chr(2,1,msg1[0]) ' print digit.1
LCD_Chr(2,2,msg1[1]) ' print char.H
LCD_Chr(2,3,msg1[2]) '
LCD_Chr(2,4,msg1[3]) '
LCD_Chr(2,5,Unit) ' print char.H
'
if M106 and L1 and not M6 then
T0=T0+1 'page stay timer
M6=1
end if
if not L1 then
M6=0
end if
if M106 and (T0>10)then
M106=0
T0=0
end if
end sub
'********************************************************************
sub procedure Signal_calculate
D22=(ADC_Read(6)/34) 'ADC_Read(6)= P1
if(D22<1)then
D22=1 ' to prevent from going to zero
end if
D21=(ADC_Read(5)*0.04) ' for Ampere (0-512 bit)/ (0-10A)
D100=(ADC_Read(5)*DCAL)' for display in decimal point (float)
D23=(ADC_Read(5)*9.79) ' millivolt reading(0-1023)/ (0-10000)
D25=(D100/D24*100) 'For Bargraph use
'
'Calibration conversion
if M107 then
DCAL=Dset/ADC_Read(5)
end if
'
'for Calibrate value write EEPROM '
if M107 then
temp=Lo(DCAL)
EEPROM_Write($12,temp)
temp=Hi(DCAL)
Delay_ms(50)
EEPROM_Write($13,temp)
temp=Higher(DCAL)
Delay_ms(50)
EEPROM_Write($14,temp)
temp=Highest(DCAL)
Delay_ms(50)
EEPROM_Write($15,temp)
M107=0
end if
'
'Max.Peak capture
if(D100>D24)then
M101=1
else
M101=0
end if
if M101 then
D24=D100
end if
end sub
'********************************************************************
sub procedure input_output
if SW3 then ' SW3 OK
L3=1 ' UART Enable bit
end if

'
if not SW1 then
M106=1 'Max.Page selection bit
end if
if(D100<D0)or(D100>D1)then
OUT_K1=1
else
OUT_K1=0
end if
end sub
'******************************************************************
sub procedure Rx_Reception
if (UART1_Data_Ready() = 1)then
UART1_Read_Text(ReceiveBuffer, ' Receive the message from the board
TerminationString, ' Receive until Termination String occures
STRING_MAX_SIZE)
end if
'
if(ReceiveBuffer[0]="C")and(ReceiveBuffer[1]="=")then ' Incoming string starts with C for CALIBRATE
StringA=(ReceiveBuffer[2]+ReceiveBuffer[3]+ReceiveBuffer[4]+ReceiveBuffer[5]+ReceiveBuffer[6]) '
Dset=Str2float(StringA)
M107=1
end if
'
if(ReceiveBuffer[0]="L")and(ReceiveBuffer[1]="=")then ' Incoming string starts with L for LOW
StringB=(ReceiveBuffer[2]+ReceiveBuffer[3]+ReceiveBuffer[4]+ReceiveBuffer[5]+ReceiveBuffer[6]) '
D0=Str2float(StringB)
M108=1
end if
'
if(ReceiveBuffer[0]="H")and(ReceiveBuffer[1]="=")then ' Incoming string starts with H for HIGH
StringC=(ReceiveBuffer[2]+ReceiveBuffer[3]+ReceiveBuffer[4]+ReceiveBuffer[5]+ReceiveBuffer[6]) '
D1=Str2float(StringC)
M109=1
end if
'
if(ReceiveBuffer[0]="U")and(ReceiveBuffer[1]="=")then ' Incoming string starts with U for UNIT
Unit=ReceiveBuffer[2] '
M110=1
end if
'
if M107 or M108 or M109 or M110 then
delay_ms(3000)
ReceiveBuffer[0]=0 ' Clears the Buffer
L3=0 '
end if
end sub
'********************************************************************
'LCD Drive bits (4-Bit Mode)
dim LCD_EN as sbit at RD5_bit
dim LCD_RS as sbit at RD4_bit
dim LCD_D4 as sbit at RD0_bit
dim LCD_D5 as sbit at RD1_bit
dim LCD_D6 as sbit at RD2_bit
dim LCD_D7 as sbit at RD3_bit
'
LCD_EN_Direction as sbit at TRISD5_bit
LCD_RS_Direction as sbit at TRISD4_bit
LCD_D4_Direction as sbit at TRISD0_bit
LCD_D5_Direction as sbit at TRISD1_bit
LCD_D6_Direction as sbit at TRISD2_bit
LCD_D7_Direction as sbit at TRISD3_bit


' DEVICE 16F877A 8mhz
' FUNCTION: Ampere Meter Relay for GPPL filter motors (0-10A)
' Last update & test via PFC-D11-1A V2.20 board on: 14.08.2016.

sub procedure interrupt ' Timer1 interrupt(every 0.5 sec.oscillating)
if(TMR1IF_bit) then ' if Overflow bit HIGH *************
T2=T2+1' *
if(T2>=2)then ' *
L1=not L1 ' *
M104=not M104 '*
T2=0 ' *
end if ' *
TMR1IF_bit= 0 ' Reset Overflow bit *
end if ' *
end sub '***********************************************

main:
ADCON1=0x0f 'PORTA inputs all are digital
ADCON0=0x00 'Disable analog functions
CMCON=0x07 'Turn off Comparators
CCP1CON=0x00'Turns off Capture/ Compare/PWM
CCP2CON=0x00'
CCPR1L=0x00'
CCPR1H=0x00'
CCPR2L=0x00
CCPR2H=0x00'
TRISA=0xff '00111111
PORTA=0XFF '
TRISD=0x00 '00000000
PORTD=0X00 '
TRISC=0x39 '11111001
PORTC=0X39 '
TRISB=0x3C '00111100
PORTB=0X3C '
TRISE=0x03 '00000011
T1CON= 0x031 ' Timer 1(16-bit) usage control bits **********
TMR1IF_bit= 0 '// // // *
TMR1H= 0x0B ' 0B *
TMR1L= 0xDC ' DC *
TMR1IE_bit= 1 ' *
INTCON =0xC0 '*****************************************
UCFG.4=0 'ADD
UCON.3=0 ' ADD
M100= 0 '
M101=0 '
M0=0 '
M1=0 '
M53=0 ' HOURMETER USE
M55=0 ' HOURMETER USE
D0=0.00 ' LOW SETPOINT VALUE
D1=0.00 ' HIGH SETPOINT VALUE
C0=0 '
C1=0 '
C2=0 '
C16=0 ' HOURMETER USE
T0=0 '
T1=0 '
T3=0 '
D22=0 '
D24=0 '
D100=0 '
OUT_K1=0 '
M103=0 '
M106=0 '
L1=0 '
L3=0 '
Lcd_Init() '
Lcd_Cmd(_LCD_CLEAR) ' Clear display
Lcd_Cmd(_LCD_CURSOR_OFF) ' Cursor off
msg1=dsp18
if(C0=0)then
msg1=dsp7
LCD_Out(1,1,msg1)
msg1=dsp8
LCD_Out(1,10,msg1)
LCD_Chr(2,5,Unit)' print char.H
CustomChar(2,15)'
CustomChar(2,14)'
CustomChar(2,13)'
CustomChar(2,12)'
CustomChar(2,11)'
CustomChar(2,10)'
CustomChar(2,9)'
CustomChar(2,8)'
CustomChar(2,7)'
CustomChar(2,6)'
CustomChar3(2,16)'
TerminationString[0] = 13
TerminationString[1] = 10
TerminationString[2] = 0
ReceiveBuffer[0]=0 ' Clear receive buffer
end if
ADC_Init() '
UART1_Init(2400)


'EEPROM DCAL Read
Lo(DCAL)=EEPROM_Read($12)' calibrate setting value
Hi(DCAL)=EEPROM_Read($13)
Higher(DCAL)=EEPROM_Read($14)
Highest(DCAL)=EEPROM_Read($15)
'
'EEPROM D0 Read
Lo(D0)=EEPROM_Read($16)' Low setting value
Hi(D0)=EEPROM_Read($17)
Higher(D0)=EEPROM_Read($18)
Highest(D0)=EEPROM_Read($19)
'
'EEPROM D1 Read
Lo(D1)=EEPROM_Read($20)' High setting value
Hi(D1)=EEPROM_Read($21)
Higher(D1)=EEPROM_Read($22)
Highest(D1)=EEPROM_Read($23)
'
'EEPROM Unit read
Lo(Unit)=EEPROM_Read($24)' High setting value

'*******************************************
while (true) ' Start of the program
'*******************************************
select case M106
case 0
LCD_Firstpage()
end select
'
'*******************************************
'LCD_Printout (Bargraph for actual value/ maximum captured value)
if(D25>10)then
CustomChar2(2,6)
else
CustomChar(2,6)
end if
'
if(D25>20)then
CustomChar2(2,7)
else
CustomChar(2,7)
end if
'
if(D25>30)then
CustomChar2(2,8)
else
CustomChar(2,8)
end if
'
if(D25>40)then
CustomChar2(2,9)
else
CustomChar(2,9)
end if
'
if(D25>50)then
CustomChar2(2,10)
else
CustomChar(2,10)
end if
'
if(D25>60)then
CustomChar2(2,11)
else
CustomChar(2,11)
end if
'
if(D25>70)then
CustomChar2(2,12)
else
CustomChar(2,12)
end if
'
if(D25>80)Then
CustomChar2(2,13)
else
CustomChar(2,13)
end if
'
if(D25>90)then
CustomChar2(2,14)
else
CustomChar(2,14)
end if
'
if(D25>95)then
CustomChar2(2,15)
else
CustomChar(2,15)
end if
'****************************************************************
'Calculations for min setpoint value derived from Trimpot P1,AI & Max.P
select case M0
case 0
Signal_calculate
end select
'*******************************************
'IO Subroutine
select case M0
case 0
input_Output()
end select
'***************************************************
'Low Value Setting
select case M0
case 0
Rx_Reception()
end select
'**********************************************************
'Low Value Setting
select case M0
case 0
Low_High_Unit_set()
end select
'**********************************************************
'Max.value page
select case M106
case 1
Max_value_page()
end select
wend
end.

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: RE0 analog stopped working when receive string 18f4550

#4 Post by Aleksandar.Mitrovic » 24 Nov 2016 09:44

Hi,

Can you please tell me which library are you using for "str2float"?

How can I test this code, which strings you are sending over UART?

Kind regards,
Aleksandar

Post Reply

Return to “mikroBasic PRO for PIC General”