Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

RF modules using mikrobasic and PIC mcu's

Status
Not open for further replies.

ktone11

Newbie level 1
Joined
Dec 7, 2013
Messages
0
Helped
0
Reputation
0
Reaction score
0
Trophy points
0
Activity points
44
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:


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

  • pp.pdf
    32.1 KB · Views: 85
Last edited by a moderator:

Everything seems right in your code.
Some time ago i also made the same project using same method, but i wrote different code. I used PIC18F248

Here is the code i used.

Reciever

Code C - [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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <p18cxxx.h>
# pragma config OSC = HS, OSCS = OFF
# pragma config WDT = OFF
# pragma config BORV = 45, PWRT = ON, BOR = ON
# pragma config DEBUG = OFF, LVP = OFF, STVR = OFF
 
unsigned int i=0;
#define RS PORTBbits.RB2
#define E PORTBbits.RB3
 
//delay
void del(unsigned long int);
 
//serial functions
void serial_init(int); //initialising serial port at 19200 baudrate
char recchar();  //recieving character
 
//LCD functions
void lcd_init(); //initialising LCD 16x2 at 4bit mode
void lcmd(unsigned char); //sending command
void ldata(unsigned char); //printing character
 
//MAIN
void main(void)
{
char c; 
lcd_init();
 serial_init(16);
 
 while(1)
 {
   c=recchar();
   ldata(c);
 }
} 
 
void del(unsigned long int k)
{
 for(i=0;i<=k*10;i++);
}
void serial_init(int i) 
{
 TXSTA=0x24;
 if(i==16)
  SPBRG=51;
 else if(i==4)
  SPBRG=12;
 RCSTA=0x90;
}
char recchar() 
{
    while(PIR1bits.RCIF==0);
    return RCREG;
}
 
void lcd_init()
{
 TRISB=0x00;
 lcmd(0x33);
 lcmd(0x32);
 lcmd(0x28); 
 lcmd(0x0E); 
 lcmd(0x01); 
 lcmd(0x06); 
 lcmd(0x80); 
}
void lcmd(unsigned char cmd)
{
 PORTBbits.RB2=0;del(2);
 PORTB = (PORTB & 0x0F)|(cmd & 0xF0);del(2) ;
 PORTBbits.RB3=1;del(2);PORTBbits.RB3=0;del(2);
 PORTB = (PORTB & 0x0F)|(cmd <<4); del(2);
 PORTBbits.RB3=1;del(2);PORTBbits.RB3=0;del(2);
 del(10);
 }
void ldata(unsigned char data)
{
 PORTBbits.RB2=1;del(2);
 PORTB = (PORTB & 0x0F)|(data & 0xF0); del(2);
 PORTBbits.RB3=1;del(2);PORTBbits.RB3=0; del(2);
 PORTB = (PORTB & 0x0F)|(data <<4);del(2);
 PORTBbits.RB3=1;del(2);PORTBbits.RB3=0; del(2);
 del(10);
}
 
void lnum(int q)
{
 char a[10]; 
 int j;
 if(q<0)
  {
   ldata('-');
   q=q*(-1);
  }
 if(q==0)
  ldata('0');
 else
 {
  for(i=0;q>0;++i)
   {
     a[i]=q%10+48;
     q=q/10;
   }
  for(j=i-1;j>=0;--j)
     ldata(a[j]);
 }
}
void lstr(char *s)
{
 while(*s)
  {
   ldata(*s);
   s++;
  }
}


Transmitter

Code C - [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
void adc_init()
{
 TRISA=0xFF;
 ADCON0=0b00000001;
 ADCON1=0b10000010;
}
int adcread()
{ int i;
 while(1)
  {
   del(1);
   ADCON0bits.GO=1;
   while(ADCON0bits.GO!=0);
   i=(ADRESH*255)+ADRESL;
   return i;
  }
}
void main(void)
{
 T1CON = 0b00000001;
 adc_init();
 serial_init();
 char c;int i;
 while(1)
 { 
   i=adcread();
   lnum(i);
   sendnum(i);
   del_sec(1);
 } 
}



Use the same function definitions for both the codes. They communicate at baud rate of 19200 and work at 16Mhz osc.
 
Last edited by a moderator:

Hi 91divine, thanks for your reply. I just wanted to know what compiler you used with the code. I tried it with Mikroc and it doesn't work
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top