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.

serial programming of microcontroller

Status
Not open for further replies.

malik ahsan

Newbie level 5
Newbie level 5
Joined
Jan 4, 2013
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,360
i want to send data using rs232 in pic16F877A....please help me out with a simple program...and on receiver end i want to receive data in matlab GUI....
 

i have written simple program and have little knowledge about pic c compiler....just tell me the commands for transmitt and receive...after using #use rs232(baud,parity,data) etc....if i just have to send a high or 1 then what would be the 2,3 commands used...
 

Code:
TXREG = TX_BYTE;
while(!TRMT);

Transmit will happened where TX_BYTE is the data.

MathLab GUI...you have to check this yourself.
 
Last edited by a moderator:

i am transmitting data using printf / fprintf in pic c compiler.....and at the receiving end in MATLAB gui , i am receiving some special characters like $a#e@ etc.....while before pc to pc communication was build up successfully using RF transceivers (data was received and transmitted in matlab) but when i connected PIC 16F877A at one end then this problem is happening as i mentioned.... matlab garbage.JPG
 


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
#include<htc.h>
 
void uart_init(void);
void TX(unsigned char TX_BYTE);
 
unsigned char a;
void main()
{
uart_init();
while(1);
}
 
void uart_init(void)
{
TRISC7 = 1;
TRISC6 = 0;
SPBRG = 5;     // 115200 BRGH = 1; 11.0592 Mhz
//    SPBRG = 129; // 9600 brgh = 1; 20 Mhz
//    SPBRG = 64;  // 19200 brgh = 1; 20 Mhz
TXSTA = 0×24;
RCIE =1 ; GIE =1; PEIE =1;
RCSTA = 0×90;
}
 
void interrupt receive(void)
{
if(RCIE && RCIF)
{
RCIF = 0;
TX(RCREG);
}
}
 
void TX(unsigned char TX_BYTE)
{
TXREG = TX_BYTE;
while(!TRMT);
}

 
Last edited by a moderator:


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
#include <16F877A.h>            
#device adc=8
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD(In Circuit Debbuging)
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage programing, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES WRT_50%                  //Lower half of Program Memory is Write Protected
#use delay(clock=16000000)      //Frequency 20MHz
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)           //Declaring USART
#define LCD_TYPE 2              //Using 16x2 Alphanumeric LCD                              
#include <lcd.c>
float x,y;
int a;  
#int_EXT
void  EXT_isr(void) 
{
         printf("1\n\r");
         delay_ms(100);
         printf("Emergency at 1\n\r");
         output_high(pin_B4);
         output_high(pin_B5);
         printf(lcd_putc,"\fEmergency at 1\r");
         delay_ms(6000);
         output_low(pin_B4);
         output_low(pin_B5);
}
#int_TIMER1
void  TIMER1_isr(void)
{
          x=((read_adc()/255.0)*5.00);             //Using 8-bit ADC with external reference of 5V
          printf("%f\n\r",x);                      //Reading ADC value                               
          printf(lcd_putc,"\fVoltage=%f V",x);     //Displaying Voltage on LCD
          y=(x/5)*100;                             //Calculating Percentage of remaining charge
          printf(lcd_putc,"\nRemaining=%f",y);     //Displaying Remaining voltage
          delay_ms(12000);
}
void main()
{
   lcd_init();
   setup_adc_ports(AN0_VREF_VREF);
   setup_adc(ADC_CLOCK_DIV_64);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_4);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_EXT);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
   ext_int_edge(H_TO_L);
   clear_interrupt(int_timer1);
   while(1)
          {
          a=getc();                
if (a==49)
   {
   printf(lcd_putc,"\fEmergency at 1\n\r");
   output_high(pin_B5);
   output_high(pin_B4);
   delay_ms(1000);
   output_low(pin_B5);
   output_low(pin_B4);
   }
if (a==50)
   {
   printf(lcd_putc,"\fEmergtency at 2\n\r");
   delay_ms(1000);
   printf("2\n\r");
   output_high(pin_B6);
   output_high(pin_B4);  
   delay_ms(1000);
   output_low(pin_B6);
   output_low(pin_B4);
   }
if (a==51)
   {
   printf(lcd_putc,"\fEmergtency at 3\n\r");
   delay_ms(1000);
   printf("3\n\r");
   output_high(pin_B7);
   output_high(pin_B4);
   delay_ms(1000);
   output_low(pin_B7);
   output_low(pin_B4);
   }
          }          
}

 
Last edited by a moderator:


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
#include <16F877A.h>            
#device adc=8
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD(In Circuit Debbuging)
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage programing, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES WRT_50%                  //Lower half of Program Memory is Write Protected
#use delay(clock=16000000)      //Frequency 20MHz
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)           //Declaring USART
#define LCD_TYPE 2              //Using 16x2 Alphanumeric LCD                              
#include <lcd.c>
float x,y;
int a;  
#int_EXT
void  EXT_isr(void) 
{
         printf("1\n\r");
         delay_ms(100);
         printf("Emergency at 1\n\r");
         output_high(pin_B4);
         output_high(pin_B5);
         printf(lcd_putc,"\fEmergency at 1\r");
         delay_ms(6000);
         output_low(pin_B4);
         output_low(pin_B5);
}
#int_TIMER1
void  TIMER1_isr(void)
{
          x=((read_adc()/255.0)*5.00);             //Using 8-bit ADC with external reference of 5V
          printf("%f\n\r",x);                      //Reading ADC value                               
          printf(lcd_putc,"\fVoltage=%f V",x);     //Displaying Voltage on LCD
          y=(x/5)*100;                             //Calculating Percentage of remaining charge
          printf(lcd_putc,"\nRemaining=%f",y);     //Displaying Remaining voltage
          delay_ms(12000);
}
void main()
{
   lcd_init();
   setup_adc_ports(AN0_VREF_VREF);
   setup_adc(ADC_CLOCK_DIV_64);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_4);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_EXT);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
   ext_int_edge(H_TO_L);
   clear_interrupt(int_timer1);
   while(1)
          {
          a=getc();                
if (a==49)
   {
   printf(lcd_putc,"\fEmergency at 1\n\r");
   output_high(pin_B5);
   output_high(pin_B4);
   delay_ms(1000);
   output_low(pin_B5);
   output_low(pin_B4);
   }
if (a==50)
   {
   printf(lcd_putc,"\fEmergtency at 2\n\r");
   delay_ms(1000);
   printf("2\n\r");
   output_high(pin_B6);
   output_high(pin_B4);  
   delay_ms(1000);
   output_low(pin_B6);
   output_low(pin_B4);
   }
if (a==51)
   {
   printf(lcd_putc,"\fEmergtency at 3\n\r");
   delay_ms(1000);
   printf("3\n\r");
   output_high(pin_B7);
   output_high(pin_B4);
   delay_ms(1000);
   output_low(pin_B7);
   output_low(pin_B4);
   }
          }          
}


You must receive the data through USART only right?

What's the purpose of getc(); here... Also printf(); function 'll never print the data on LCD... can you please tell me the scope of your program?
 

getc() for receiving data and printf() for transmitting data......i have to transmitt numeric or alphanumeric data from one point to another ....one terminal is pic connected with RF transceiver and other terminal is pc connected with RF transceiver....on pc i have to get data in MATLAB....
 

yetserday i got successful in transmitting alphanumeric data from PIC16F877A to the MATLAB in PC through HR-1029 RF Transceivers....but vice versa is not working now....
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top