简体   繁体   中英

UART pic18 to pic18

Cant send character 'C' and display on receiver pic. LCD prints some special characters, no 'C'... ;(

Transmiter:

char uart_rd;
int uart_rdi;

sbit LCD_RS at LATB0_bit;
sbit LCD_EN at LATB1_bit;
sbit LCD_D4 at LATB5_bit;
sbit LCD_D5 at LATB4_bit;
sbit LCD_D6 at LATB3_bit;
sbit LCD_D7 at LATB2_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;


void main() {
  ANSELA  = 0;                     // Configure AN pins as digital
  ANSELB = 0;
  ANSELC = 0;

  Lcd_Init();

  UART2_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize



 while (1) {

   UART2_Write('C');
   }
 }

Receiver:

char uart_rd;
int uart_rdi;

sbit LCD_RS at LATB0_bit;
sbit LCD_EN at LATB1_bit;
sbit LCD_D4 at LATB5_bit;
sbit LCD_D5 at LATB4_bit;
sbit LCD_D6 at LATB3_bit;
sbit LCD_D7 at LATB2_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;


void main() {
  ANSELA  = 0;                     // Configure AN pins as digital
  ANSELB = 0;
  ANSELC = 0;

  Lcd_Init();

  UART2_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize


 while (1) {
    if (UART2_Data_Ready()) {     // If data is received,
     uart_rdi = UART2_Read();     // read the received data,
       uart_rd = uart_rdi;
      LCD_Out(1,1, uart_rd);

   }
 }
}

Unsing two pics 18F26K22, new to serial ports... dont know how to test it as dont have equipment, is my code is good? Im not using Proteus, working on metal. THANKS.

I had once this problem, and the fact was that the character was not a unsigned char. so try to cast it.

...
while (1) {
if (UART2_Data_Ready()) {     // If data is received,
  uart_rdi = UART2_Read();     // read the received data,
  uart_rd = uart_rdi;
  LCD_Out(1,1, (unsigned char) uart_rd);
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM