ktone11
Newbie level 1
I'm trying to program transmitter and receiver to transmit a temperature reading on an LCD.
The PIC transmitter seems to be working ok on proteus simulation, but receiving nothing on the receiver using a single wire using tx and rx UART.
Here is my code:
TRANSMITTER:
RECEIVER:
The PIC transmitter seems to be working ok on proteus simulation, but receiving nothing on the receiver using a single wire using tx and rx UART.
Here is my code:
TRANSMITTER:
Code Basic4GL - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 program tx Dim i1, j1, j2, CS as byte Dim Text as string[20] Dim Deg as string[3] Dim HalfDeg as byte ' This subroutine calculates one byte Checksum Sub function CheckSum(dim byref cadena as string[20])as byte Dim i2 as byte Dim j3 as word j3 = 0 For i2 = 0 to StrLen(cadena)-1 j3 = j3 + cadena[i2] next i2 result = Lo(j3) ' Truncate Checksum to one byte End sub Main: ADCON0.0=0 ' Shut-off ADC module ADCON1=%00000110 ' PORTA pins digital PORTA = 255 ' PORTA high TRISA = 255 ' PORTA is input UART1_Init(19200) ' Initialize at 19200 Baud (the default baud rate for RF module) while true ' Endless loop. for i1 = 0 to 19 ' Initialize string Text Text[i1] = Chr(0) next i1 ' One Wire operation Ow_Reset(PORTA,0) ' Reset One Wire ' (pin 2 (RA0/AN0) of PIC 16F876 connected with pin2 of DS1820) OW_Write(PORTA, 0, $CC) ' Send command to DS1820 (SKIP ROM) OW_Write(PORTA, 0, $44) ' Send command to DS1820 (INIT CONVERSION) Delay_us(120) Ow_Reset(PORTA,0) ' Reset OW_Write(PORTA, 0, $CC) ' Send command to DS1820 (SKIP ROM) OW_Write(PORTA, 0, $BE) ' Send command to DS1820 (READ SCRATCHPAD) j1 = OW_Read(PORTA, 0) ' Get the result (temperature Low byte) j2 = OW_Read(PORTA, 0) ' Get the result (temperature High byte) if j2>0 then j1 = -j1 Text[0] = 45 ' ASCII code for "-" (minus sign) else ' j1 = j1 Text[0] = 43 ' ASCII code for "+" (plus sign) end if ByteToStr((j1 div 2), Deg) If j1 mod 2 = 0 then HalfDeg = 48 ' ASCII code for number "0" else HalfDeg = 53 ' ASCII code for number "5" end if ' Prepare the string to be transmitted: Text[1] = Deg[0] ' Add temperature Text[2] = Deg[1] Text[3] = Deg[2] Text[4] = "." ' Add decimal point Text[5] = HalfDeg ' Add half degree = 5 or 0 CS = CheckSum(Text) ' Calculate Checksum of string data Text[6] = CS ' Add Checksum to string Text Text[7] = "O" ' Add delimiter OK Text[8] = "K" UART1_Write_Text(Text) ' Send string Text via USART and RF Delay_ms(2000) wend End.
RECEIVER:
Code Basic4GL - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 program uart dim LCD_RS as sbit at RB4_bit LCD_EN as sbit at RB5_bit LCD_D4 as sbit at RB0_bit LCD_D5 as sbit at RB1_bit LCD_D6 as sbit at RB2_bit LCD_D7 as sbit at RB3_bit LCD_RS_Direction as sbit at TRISB4_bit LCD_EN_Direction as sbit at TRISB5_bit LCD_D4_Direction as sbit at TRISB0_bit LCD_D5_Direction as sbit at TRISB1_bit LCD_D6_Direction as sbit at TRISB2_bit LCD_D7_Direction as sbit at TRISB3_bit Dim LenText, CSreceived, CScalculated as byte Dim Text as String[20] Dim i as byte ' This subroutine calculates one byte Checksum Sub function CheckSum(dim byref text2 as string[20])as byte Dim i2 as byte Dim j as word j = 0 For i2 = 0 to StrLen(text2)-1 j = j + text2[i2] next i2 result = Lo(j) 'Checksum truncated to one byte End sub Main: Ansel=0 'Digital AnselH=0 'Digital ADCON0=0 'Turns off ADC UART1_Init(19200) ' Initialize to 19200 Bd (the default baud rate for RF module) Lcd_Init() ' Lcd Init for EasyPic5 LCD_Cmd(_LCD_CLEAR) LCD_Cmd(_Lcd_Return_Home) LCD_Cmd(_LCD_CURSOR_OFF) Delay_ms(200) while TRUE ' Endless loop for i = 0 to 19 ' Initialize string Text Text[i] = Chr(0) next i while UART1_Data_Ready() = 0 NOP wend ' If there is no data, wait UART1_Read_Text(Text,"OK", 10) ' Read received data via RF and USART ' until OK is received Delay_ms(200) LenText = StrLen(Text) CSreceived = Text[LenText-1] ' Read received Checksum Text[LenText-1] = Chr(0) ' Remove Checksum of the string Text CScalculated = CheckSum(Text)' Calculate Checksum of received data If CSreceived = CScalculated ' If the received Checksum and the ' calculated Checksum matches, then... then Lcd_Out(1, 1, "Temperature:") Lcd_Out(2, 1, Text) ' Display temperature Lcd_Chr(2, 9, 223) ' Display "º" Lcd_Chr(2, 10, "C") ' Display "C" else ' If the received Checksum and the calculated Checksum ' doesn't match, the data will be discarded Lcd_Out(1, 1, "Temperature:") end if Delay_ms(1000) LCD_Cmd(_LCD_CLEAR) ' Clear LCD Lcd_Out(1, 1, "Temperature:") wend End.
Attachments
Last edited by a moderator: