使用UART串行通信将4个微控制器连接到PC

时间:2019-02-12 19:18:15

标签: microcontroller uart microchip mikroc

**是否有一种方法可以使用UART将4个微控制器连接在一起,即使每个微控制器中都有一个UART模块(18f452)也可以将其中三个的数据发送到主模块(18f4550)以在PC上显示

switching using and gates Img

microcontrollers with uart conn Img

主UC“ U4”的mikroC代码

   // LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
char u1[20], u2[20], u3[20];
void newline()
{
  Uart1_write (0x0D);
  Uart1_write (0x0A);
}
void main() {
  TRISD = 0x00;
  Uart1_Init(9600);
  delay_ms(20);                  // Wait for UART module to stabilize

  Lcd_Init();                        // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
while(1){
  portd=0x01;
  UART1_Read_Text(u1, "ok", 20);
  portd=0x02;
  UART1_Read_Text(u2, "OK", 20);
  portd=0x04;
  UART1_Read_Text(u3, "OK", 20);
  Lcd_Out(1,5,u1);
  Delay_ms(1000);
  Lcd_Out(1,5,u2);
  Delay_ms(1000);
  Lcd_Cmd(_LCD_CLEAR);
  Delay_ms(1000);
  Lcd_Out(1,5,u3);
  delay_ms(250);       
  portd=0x08;
UART1_Write_Text(u1);
        newline();
        newline();
UART1_Write_Text(u2);
        newline();
        newline();
UART1_Write_text(u3);
        newline();
        newline();   
  }
}

用于奴隶的mikroC代码

char txt[20];
int Vin;
float Dis_volt;
void main()
{
     TRISA = 0xFF;
     ADCON1 = 0x80;
     uart1_Init (9600);
     while(1)
      {
      Vin = adc_read(0);
      Dis_volt = (Vin * 500)/ 1024;
      floatToStr (Dis_volt , txt);
      txt[5]='o';
      txt[6]='k';
      Uart1_write_Text(txt);
     }
}

0 个答案:

没有答案