佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

楼主: pikachew

请问RS232能显示BREADBOARD上的资料吗?

  [复制链接]
 楼主| 发表于 13-10-2011 11:28 PM | 显示全部楼层
回复  pikachew


看了你的图和源码, 发现你是把input接去PortB, LCD 又接去Port B, 就有冲突, 所 ...
pic 发表于 13-10-2011 07:42 PM

我的PROJECT是一个停车场。
LCD是接去PORT B , LED是接去PORT D。 我已经修改。
1.当车子/障碍物 在LDR 上面时,LED 就会变暗。如果没有,就会亮。
2.LDR 的光源是灯光(白灯)。
3.LED,目的是要显示 有多少个PARKING LOT 被OCCUPIED 了 和 哪一个 PARKING LOT 还AVAILABLE?
如:PARKING LOT1 AVAILABLE, PARKING LOT2 AVAILABLE.(LDR是INPUT)
4. LED 是作为LDR 的光源?不是。
5. 还是LED 是要显示LDR 的状态?可以这么说。

刚刚试了#99的CODE.
之前的RESULT 是
NO1,NO1 YES1
现在变成
NO1,NO1,YES1,YES1.
所以我现在还在尝试不同的DELAY_ms
谢谢。
回复

使用道具 举报


ADVERTISEMENT

发表于 13-10-2011 11:43 PM | 显示全部楼层
本帖最后由 pic 于 14-10-2011 12:28 AM 编辑
2.LDR 的光源是灯光(白灯)。
3.LED,目的是要显示 有多少个PARKING LOT 被OCCUPIED 了 和 哪一个 PARKING LOT 还AVAILABLE?
pikachew 发表于 13-10-2011 11:28 PM

那么, 如果你是按你提供的电路的话, 那么你的电路错了。
你的LDR 的电, 不可以通过PortB 来供电, 应该是直接用5V。

下面是简化的图。16F877A 用20Mhz, 如果你用4Mhz 就在程序里改。



  1.        #include "16f877a.h"
  2.     #fuses HS,PROTECT,NoWDT,put,brownout
  3.     #use delay(clock=20000000)
  4.     #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
  5.    #define LCD_DATA_PORT getenv("SFR:PORTD")   
  6. #include "LCD-pikachew.C"


  7.     int1 fiA0=0; // Flag
  8.     int1 fiA1=0;

  9.     void main()
  10.     {
  11.       lcd_init();
  12.       lcd_putc("\fCar Park Sys");
  13.       lcd_putc("\nV1.00 ");
  14.       delay_ms(2000);
  15.       lcd_putc("\f"); // clear LCD screen

  16.       fiA0=input(pin_a0); // initialize
  17.       fiA1=input(pin_a1); // initialize


  18.        while(1)
  19.        {

  20.           if(fiA0!=input(pin_a0)) // Test if Flag is change status
  21.           {
  22.              fiA0=input(pin_a0); // lock Flag
  23.            delay_ms(100); // debounce
  24.              if(input(pin_a0))
  25.              {
  26.                 output_low(pin_b0);
  27.                 printf("No1\n\r");


  28.             lcd_putc("\fone");

  29.              }
  30.              else
  31.              {
  32.                 output_high(pin_b0);
  33.                 printf("yes1\n\r");
  34.          
  35.             lcd_putc("two");
  36.              }
  37.           }      
  38.          
  39.           if(fiA1!=input(pin_a1)) // Test Flag
  40.           {
  41.              fiA1=input(pin_a1); // lock Flag
  42.             delay_ms(100); // debounce
  43.              if(input(pin_a1)==1)
  44.              {
  45.                 output_low(pin_b1);
  46.                 printf("No2\n\r");

  47.             lcd_putc("\nthree");
  48.              }
  49.              else
  50.              {
  51.                 output_high(pin_b1);
  52.                 printf("yes2\n\r");
  53.    
  54.             lcd_putc("\nfour");
  55.                 }
  56.           }
  57.        }
  58.     }


复制代码



下面是"LCD-pikachew.C"


  1. // "LCD-pikachew.C"

  2. struct lcd_pin_map {                 // This structure is overlayed
  3.            BOOLEAN enable;           // on to an I/O port to gain
  4.            BOOLEAN rs;               // access to the LCD pins.
  5.            BOOLEAN rw;               // The bits are allocated from
  6.            BOOLEAN unused;           // low order up.  ENABLE will
  7.            int     data : 4;         // be pin B0.
  8.         } lcd;


  9. #if defined(__PCH__)
  10. #if defined use_portb_lcd
  11.    #byte lcd = 0xF81                   // This puts the entire structure
  12. #else
  13.    #byte lcd = 0xF83                   // This puts the entire structure
  14. #endif
  15. #else
  16. #if defined use_portb_lcd
  17.    #byte lcd = 6                  // on to port B (at address 6)
  18. #else
  19.    #byte lcd = 8                 // on to port D (at address 8)
  20. #endif
  21. #endif

  22. #if defined use_portb_lcd
  23.    #define set_tris_lcd(x) set_tris_b(x)
  24. #else
  25.    #define set_tris_lcd(x) set_tris_d(x)
  26. #endif


  27. #define lcd_type 2           // 0=5x7, 1=5x10, 2=2 lines
  28. #define lcd_line_two 0x40    // LCD RAM address for the second line


  29. BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
  30.                              // These bytes need to be sent to the LCD
  31.                              // to start it up.


  32.                              // The following are used for setting
  33.                              // the I/O port direction register.

  34. struct lcd_pin_map const LCD_WRITE = {0,0,0,0,0}; // For write mode all pins are out
  35. struct lcd_pin_map const LCD_READ = {0,0,0,0,15}; // For read mode data pins are in



  36. BYTE lcd_read_byte() {
  37.       BYTE low,high;
  38.       set_tris_lcd(LCD_READ);
  39.       lcd.rw = 1;
  40.       delay_cycles(1);
  41.       lcd.enable = 1;
  42.       delay_cycles(1);
  43.       high = lcd.data;
  44.       lcd.enable = 0;
  45.       delay_cycles(1);
  46.       lcd.enable = 1;
  47.       delay_us(1);
  48.       low = lcd.data;
  49.       lcd.enable = 0;
  50.       set_tris_lcd(LCD_WRITE);
  51.       return( (high<<4) | low);
  52. }


  53. void lcd_send_nibble( BYTE n ) {
  54.       lcd.data = n;
  55.       delay_cycles(1);
  56.       lcd.enable = 1;
  57.       delay_us(2);
  58.       lcd.enable = 0;
  59. }


  60. void lcd_send_byte( BYTE address, BYTE n ) {

  61.       lcd.rs = 0;
  62.       while ( bit_test(lcd_read_byte(),7) ) ;
  63.       lcd.rs = address;
  64.       delay_cycles(1);
  65.       lcd.rw = 0;
  66.       delay_cycles(1);
  67.       lcd.enable = 0;
  68.       lcd_send_nibble(n >> 4);
  69.       lcd_send_nibble(n & 0xf);
  70. }


  71. void lcd_init() {
  72.     BYTE i;
  73.     set_tris_lcd(LCD_WRITE);
  74.     lcd.rs = 0;
  75.     lcd.rw = 0;
  76.     lcd.enable = 0;
  77.     delay_ms(15);
  78.     for(i=1;i<=3;++i) {
  79.        lcd_send_nibble(3);
  80.        delay_ms(5);
  81.     }
  82.     lcd_send_nibble(2);
  83.     for(i=0;i<=3;++i)
  84.        lcd_send_byte(0,LCD_INIT_STRING[i]);
  85. }


  86. void lcd_gotoxy( BYTE x, BYTE y) {
  87.    BYTE address;

  88.    if(y!=1)
  89.      address=lcd_line_two;
  90.    else
  91.      address=0;
  92.    address+=x-1;
  93.    lcd_send_byte(0,0x80|address);
  94. }

  95. void lcd_putc( char c) {
  96.    switch (c) {
  97.      case '\f'   : lcd_send_byte(0,1);
  98.                    delay_ms(2);
  99.                                            break;
  100.      case '\n'   : lcd_gotoxy(1,2);        break;
  101.      case '\b'   : lcd_send_byte(0,0x10);  break;
  102.      default     : lcd_send_byte(1,c);     break;
  103.    }
  104. }

  105. char lcd_getc( BYTE x, BYTE y) {
  106.    char value;

  107.     lcd_gotoxy(x,y);
  108.     while ( bit_test(lcd_read_byte(),7) ); // wait until busy flag is low
  109.     lcd.rs=1;
  110.     value = lcd_read_byte();
  111.     lcd.rs=0;
  112.     return(value);
  113. }
复制代码
回复

使用道具 举报

 楼主| 发表于 15-10-2011 07:14 PM | 显示全部楼层
那么, 如果你是按你提供的电路的话, 那么你的电路错了。
你的LDR 的电, 不可以通过PortB 来供电, 应 ...
pic 发表于 13-10-2011 11:43 PM



   
  1.         if((input(pin_a0)==1) && (input(pin_a1)==1))
  2.                         {
  3.                                 delay_ms(1000);
  4.                                 lcd_putc("\fPL avialable:0");
  5.                         printF("PL avialable:0");
  6.                                                 }       

  7.                         if((input(pin_a0)==0) && (input(pin_a1)==0))
  8.                                 {
  9.                                 delay_ms(1000);
  10.                                 lcd_putc("\fPL available:2");
  11. printf("PL available:2");
  12.                                 delay_ms(2000);
  13.                                 lcd_putc("\fPL1-empty");
  14.                                 lcd_putc("\nPL2-empty");
  15.                        
  16.                                                 }       
  17.                        
  18.                         if((input(pin_a0)==1) || (input(pin_a1)==1))
  19.                 {
  20.                                 delay_ms(1000);
  21.                                 lcd_putc("\nPL available:1/2 ");
  22.                                 printf("PL available:1/2 ");
  23.                        
  24.                                         }       
  25.                
  26.        }
复制代码


现在面对一些问题
就是是否在这里也要加入FLAG呢?
因为在LCD里不需要FLAG 也没问题,可是电脑里不用FLAG的话,这CODE会导致电脑当机。
PL-PARKING LOT
这里只有两个PARKING LOTS.
谢谢。
回复

使用道具 举报

发表于 16-10-2011 09:53 AM | 显示全部楼层
因为在LCD里不需要FLAG 也没问题,可是电脑里不用FLAG的话,这CODE会导致电脑当机
pikachew 发表于 15-10-2011 07:14 PM

电脑当机? 怎样当法?蓝屏?还是什么?
回复

使用道具 举报

 楼主| 发表于 16-10-2011 02:26 PM | 显示全部楼层
电脑当机? 怎样当法?蓝屏?还是什么?
pic 发表于 16-10-2011 09:53 AM

当接取电脑SERIAL PORT是,用VB来读去得时候,就会出现蓝屏。用HYPER TERMINAL 就没事。

但是重点是我必须用VB来读取,所以如果LOOP不停的话,蓝屏就会出现。
谢谢。
回复

使用道具 举报

发表于 16-10-2011 03:28 PM | 显示全部楼层
当接取电脑SERIAL PORT是,用VB来读去得时候,就会出现蓝屏。用HYPER TERMINAL 就没事。

但是重点是我 ...
pikachew 发表于 16-10-2011 02:26 PM

呵呵。。简单来说,那是你VB的部分写到不好。。
回复

使用道具 举报

Follow Us
 楼主| 发表于 16-10-2011 06:02 PM | 显示全部楼层
本帖最后由 pikachew 于 16-10-2011 06:08 PM 编辑
呵呵。。简单来说,那是你VB的部分写到不好。。
pic 发表于 16-10-2011 03:28 PM


但是我觉得应该是WHILE LOOP所导致的
因为之前上面的几句都没用FLAG时,PRINTF在WHILE LOOP 里也是一直LOOP
因为当CONDITION是对时,它就会一直LOOP.然后加了FLAG,才能把它控制着,然后也拿到我想要的答案。
可是在 && 和 || 时,是否适合用FLAG来控制着呢??之前试过加入FLAG在这里(&& 和 ||) , 结果导致LCD 完全DISPLAY 不到东西。
现在面对一些LOGIC 问题。谢谢。

还有一个问题就是从PROTEUS IRIS SCHEMATIC 换取 AERES PCB DESIGN的问题
PIC16F877A 和 MAX 232 IC 在 IRIS DESIGN SCHEMATIC 里VCC 和 VDD 是HIDDEN PINS的,可是PCB 是却是有SHOW 出来的。那么在PCB DESIGN时要把这些PINS接去那里呢?我有GOOGLE过,可是很少这方面的教学。谢谢。
回复

使用道具 举报

 楼主| 发表于 23-10-2011 11:59 AM | 显示全部楼层
  1. if((input(pin_a0)==1) && (input(pin_a1)==1) && (input(pin_a2)==1))
  2.                         {
  3.                                 delay_ms(1000);
  4.                                 lcd_putc("\fPL avialable:0");
  5.                        
  6.                                                 }       

  7.                 else if((input(pin_a0)==0) && (input(pin_a1)==0) && (input(pin_a2)==0))
  8.                                 {
  9.                                 delay_ms(1000);
  10.                                 lcd_putc("\fPL available:3");
  11.                                 delay_ms(2000);
  12.                                 lcd_putc("\fPL1-empty");
  13.                                 lcd_putc("\nPL2-empty");
  14.                                 delay_ms(1000);
  15.                                 lcd_putc("\fPL3-empty");
  16.                                 delay_ms(1000);
  17.                        
  18.                                                 }       


  19.        

  20.                  else        if((input(pin_a0)==1) || (input(pin_a1)==1) || (input(pin_a2)==1))
  21.                 {
  22.                        
  23.                                 lcd_putc("\nPL available:2/3");
  24.                                
  25.                        
  26.                                         }       
  27.                



  28.                         else if((input(pin_a0)==0) && (input(pin_a1)==1) && (input(pin_a2)==0))
  29.                 {
  30.                        
  31.                                 lcd_putc("\nPL available:2/3");
  32.                                
  33.                        
  34.                                         }       
  35.                        
  36.                                 else if((input(pin_a0)==0) && (input(pin_a1)==1) && (input(pin_a2)==1))
  37.                 {
  38.                        
  39.                                 lcd_putc("\nPL available:1/3");
  40.                                
  41.                        
  42.                                         }       
  43.                        
  44.                                 else if((input(pin_a0)==1) && (input(pin_a1)==0) && (input(pin_a2)==0))
  45.                 {
  46.                        
  47.                                 lcd_putc("\nPL available:2/3");
  48.                                
  49.                        
  50.                                         }       


  51.                                 else if((input(pin_a0)==1) && (input(pin_a1)==0) && (input(pin_a2)==1))
  52.                 {
  53.                        
  54.                                 lcd_putc("\nPL available:1/3");
  55.                                
  56.                        
  57.                                         }       


  58.                                 else if((input(pin_a0)==1) && (input(pin_a1)==1) && (input(pin_a2)==0))
  59.                 {
  60.                        
  61.                                 lcd_putc("\nPL available:1/3");
  62.                                
  63.                        
  64.                                         }       
复制代码


小弟想问一下,请问以上的CODE可以被SIMPLIFIED 吗?
比如 不用打那么多个CONDITIONS.
谢谢。
回复

使用道具 举报


ADVERTISEMENT

发表于 24-10-2011 08:32 AM | 显示全部楼层
回复 108# pikachew


    可以。除了第一个,和第二个之外。
全都是在print string.
你可以用constant string array.
你的input是 a0 a1 a2.你可以直接read整个port, 用logical & 0x07.你就拿到a0 a1 a2. value是0-7之间。
然后用一个constant string array[], 根据你的port & 0x07,从array选你要print 的string.
回复

使用道具 举报

发表于 24-10-2011 10:42 AM | 显示全部楼层
小弟想问一下,请问以上的CODE可以被SIMPLIFIED 吗?
比如 不用打那么多个CONDITIONS.
谢谢。
pikachew 发表于 23-10-2011 11:59 AM

当然可以, 这时就是你考虑你的程序结构性的问题了。。
假设是100个, 1000个泊车位呢?
回复

使用道具 举报

 楼主| 发表于 24-10-2011 08:46 PM | 显示全部楼层
当然可以, 这时就是你考虑你的程序结构性的问题了。。
假设是100个, 1000个泊车位呢?
pic 发表于 24-10-2011 10:42 AM


那么就是说一定要用到ARRAY 和FOR LOOP 了?
比如ARRAY[100]然后一直--下去?
回复

使用道具 举报

 楼主| 发表于 25-10-2011 10:24 AM | 显示全部楼层
把它修改成这样了,可是LCD只会SHOW PL AVAILABLE:3
全部INPUT都变不到去“1”。
  1. #include "16f877a.h"
  2.     #fuses XT,PROTECT,NoWDT,put,brownout
  3.     #use delay(clock=4000000)
  4.     #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
  5. #include "lcd.c"

  6.     int1 fiA0=0; // Flag
  7.     int1 fiA1=0;
  8.         int1 fiA2=0;
  9.         int counter;

  10.     void main()
  11.     {
  12.       lcd_init();
  13.       lcd_putc("\fCar Park Sys");
  14.      
  15.       delay_ms(2000);
  16.       lcd_putc("\f"); // clear LCD screen

  17.       fiA0=input(pin_a0); // initialize
  18.       fiA1=input(pin_a1); // initialize
  19.           fiA2=input(pin_a2);
  20.                 counter = (input(pin_a0) + input(pin_a1) + input(pin_a2));
  21.        while(1)
  22.        {

  23.           if(fiA0!=input(pin_a0)) // Test if Flag is change status
  24.           {
  25.              fiA0=input(pin_a0); // lock Flag
  26.           delay_ms(100); // debounce
  27.              if(input(pin_a0)==1)
  28.              {
  29.                 output_low(pin_d6);
  30.                 printf("\nPARKING LOT 1- OCCUPIED\r");
  31.                                 lcd_putc("\fPL1-occupied");

  32.              }
  33.              else
  34.              {
  35.                 output_high(pin_d6);
  36.                 printf("\nPARKING LOT1- EMPTY\r");
  37.          
  38.             lcd_putc("\fPL1-empty");

  39.              }
  40.                        
  41.           }      
  42.          
  43.           if(fiA1!=input(pin_a1)) // Test Flag
  44.           {
  45.              fiA1=input(pin_a1); // lock Flag
  46.             delay_ms(100); // debounce
  47.              if(input(pin_a1)==1)
  48.              {
  49.                 output_low(pin_d7);
  50.                 printf("\nPARKING LOT2-OCCUPIED\r");

  51.             lcd_putc("\fPL2-occupied");

  52.                

  53.              }
  54.              else
  55.              {
  56.                 output_high(pin_d7);
  57.                 printf("\nPARKING LOT2-EMPTY\r");
  58.    
  59.             lcd_putc("\fPL2-empty");


  60.                 }
  61.           }


  62.                     if(fiA2!=input(pin_a2)) // Test Flag
  63.           {
  64.              fiA2=input(pin_a2); // lock Flag
  65.             delay_ms(100); // debounce
  66.              if(input(pin_a2)==1)
  67.              {
  68.                 output_low(pin_d5);
  69.                 printf("\nPARKING LOT3-OCCUPIED\r");

  70.             lcd_putc("\fPL3-occupied");

  71.                

  72.              }
  73.              else
  74.              {
  75.                 output_high(pin_d5);
  76.                 printf("\nPARKING LOT3-EMPTY\r");
  77.    
  78.             lcd_putc("\fPL3-empty");


  79.                 }
  80.           }


  81.                        
  82.                  if((input(pin_a0)==1) && (input(pin_a1)==1) && (input(pin_a2)==1))
  83.                         {
  84.                        
  85.                                 lcd_putc("\fPL avialable:0");
  86.                                 printf("\nParking Lot available:0");
  87.                                                 }
  88.                                
  89.                 else if((input(pin_a0)==0) && (input(pin_a1)==0) && (input(pin_a2)==0))
  90.                                 {
  91.                                 delay_ms(1000);
  92.                                 lcd_putc("\fPL available:3");
  93.                                 delay_ms(2000);
  94.                                 lcd_putc("\fPL1-empty");
  95.                                 lcd_putc("\nPL2-empty");
  96.                                 delay_ms(1000);
  97.                                 lcd_putc("\fPL3-empty");
  98.                                 delay_ms(1000);
  99.                        
  100.                                                 }
  101.                

  102.                 else if ( counter >0 && counter <3)
  103.                         {
  104.                                         lcd_putc("\fPL available:1/3");                               
  105.                                                 }                               
  106.                 else
  107.                         {
  108.                                         lcd_putc("\fPL available:2/3");               
  109.                                                 }                               
  110.                
  111.                
  112.         }       

  113. }
复制代码
当然可以, 这时就是你考虑你的程序结构性的问题了。。
假设是100个, 1000个泊车位呢?
pic 发表于 24-10-2011 10:42 AM
回复

使用道具 举报

发表于 25-10-2011 11:34 AM | 显示全部楼层
回复 112# pikachew
快速看, 看不懂你的code, 除非花时间去慢慢看

  1. counter = (input(pin_a0) + input(pin_a1) + input(pin_a2));
复制代码

上面不成立。。

考虑改成
  1. counter=0;
  2. if(input(pin_a0)) counter++;
  3. if(input(pin_a1)) counter++;
  4. if(input(pin_a2)) counter++;
复制代码
回复

使用道具 举报

 楼主| 发表于 25-10-2011 06:32 PM | 显示全部楼层
回复  pikachew
快速看, 看不懂你的code, 除非花时间去慢慢看


上面不成立。。

考虑改成
pic 发表于 25-10-2011 11:34 AM

eg.
  1. if (counter =0)
  2.                                 {
  3.                                                 lcd_putc("\fPL available:",((3-counter)/3));
  4.                                                         }       
复制代码



现在想不通为什么书本里可以这样放可是在这里就不能这样放。。。
就算加了" ) " 它还是一直有ERROR说要加CLOASE PAREN,
谢谢。
回复

使用道具 举报

发表于 25-10-2011 07:03 PM | 显示全部楼层
回复 114# pikachew

if (counter ==0)  
{
   lcd_putc( "\fPL available:  %u ", (  (3-counter)/3  )    );
}


你是要 test if Counter=0 ?
如是, 那么应该写成:if (counter ==0)  

if (counter =0)  《---这个是错的语法  


   lcd_putc( "\fPL available:", (  (3-counter)/3  )    ); 《==== 错
   lcd_putc( "\fPL available:  %u ", (  (3-counter)/3  )    );   《== 要有variable  %u
回复

使用道具 举报

 楼主| 发表于 30-10-2011 02:22 PM | 显示全部楼层
回复  pikachew

if (counter ==0)  
{
   lcd_putc( "\fPL available:  %u ", (  (3-counter)/3  )   ...
pic 发表于 25-10-2011 07:03 PM


哦哦。明白了。
  1. ' Header******************************************************
  2. program example_11 ' Program name
  3. dim i as byte      ' Variable is of byte type
  4. main:              ' Start of program
  5. UART1_Init(19200)  ' Initialize USART module
  6. ' (8 bit, 19200 baud rate, no parity bit...)

  7. while 1            ' Endless loop
  8.   if UART1_Data_Ready() then ' If data has been received
  9.     i = UART1_Read()         ' read it
  10.     UART1_Write(i)           ' and send it back
  11.   end if
  12. wend

  13. end.               ' End of program
复制代码

    这是在网上看到的CODE
这CODE能顺利读取到电脑里通过RS232传送进电脑的资料。因为不是完整有点难理解。
比如“UART1_Data_Ready()” 和 “UART1_Init(19200)”不用被DEFINE?
谢谢
http://www.mikroe.com/eng/chapters/view/77/pic-basic-book-chapter-4-examples/
回复

使用道具 举报


ADVERTISEMENT

发表于 30-10-2011 04:18 PM | 显示全部楼层
这CODE能顺利读取到电脑里通过RS232传送进电脑的资料。因为不是完整有点难理解。
比如“UART1_Data_Ready()” 和 “UART1_Init(19200)”不用被DEFINE?
谢谢
pikachew 发表于 30-10-2011 02:22 PM

PIC basic 我不会。

UART1_Data_Ready(), UART1_Init(19200) 应该是PICBasic 的内建指令。


从code 推敲:
UART1_Init(19200)
在CCS C , 是用
  1. #use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)
复制代码


UART1_Data_Ready()
当USART有data 来了。。
在CCS C , 是用
  1. if( kbhit() )
  2. {

  3. }
复制代码
回复

使用道具 举报

 楼主| 发表于 30-10-2011 06:12 PM | 显示全部楼层
PIC basic 我不会。

UART1_Data_Ready(), UART1_Init(19200) 应该是PICBasic 的内建指令。


从co ...
pic 发表于 30-10-2011 04:18 PM


哦哦
原来我还分不清楚那个是CCS C 哪个是HI TECH的。
不过怎么网上都找不到如何读取VISUAL BASIC2008通过RS232传送资料到PIC里的?
我试了这但是不能走,请问有谁会呢?
  1. if (        getchar())
  2.                                         {
  3.                                                 printf("ABCD");
  4.                                                         }                       
  5.              }
复制代码

谢谢。
回复

使用道具 举报

发表于 31-10-2011 10:01 AM | 显示全部楼层
回复 118# pikachew

  1. if (        getchar())
  2.                                         {
  3.                                                 printf("ABCD");
  4.                                                         }                        
  5.              }
复制代码


“getchar()” 这个功能在电脑C语言是无限等待键盘一字节(byte)输入。 也就是说如果没等到键盘回传值,是不会执行下一行程序的。

在MCU 这个指令被改用在 “UART”里做无限单一字节接收,用了它MCU就不用在做其它的工作了。

“getchar()”只出现在一些“MCU UART”入门教程里。



  1. ' Header******************************************************
  2. program example_11 ' Program name
  3. dim i as byte      ' Variable is of byte type
  4. main:              ' Start of program
  5. UART1_Init(19200)  ' Initialize USART module
  6. ' (8 bit, 19200 baud rate, no parity bit...)

  7. while 1            ' Endless loop
  8.   if UART1_Data_Ready() then ' If data has been received
  9.     i = UART1_Read()         ' read it
  10.     UART1_Write(i)           ' and send it back
  11.   end if
  12. wend

  13. end.               ' End of program
复制代码

这个版主已经解释很清楚了,我不在多说。
只是补充一点的是所有MCU的UART 接收和发送资料的缓冲区就只是一个字节(byte)而已。如果来不及处理接收缓冲区的资料,就有可能被下一个接收到的新资料给覆盖掉(资料丢失)。

  1. printf("\nPARKING LOT3-OCCUPIED\r");
复制代码

这个MCU发送资料给电脑function,一旦执行,也就等待发送完所有资料才会执行下一行程序。有点类似“getchar()”。


如果在高效率的要求底下,就只能运用MCU本身的中断服务(Interrupt)功能来解决了。以上的所提到function 只会降低MCU的运行效果。
回复

使用道具 举报

发表于 31-10-2011 04:46 PM | 显示全部楼层
从楼主的发问, 感觉楼主对USART 没有基本概念。。
好好看:
电脑串行端口(serial port) 和单晶片通讯
http://cforum3.cari.com.my/viewt ... &extra=page%3D1
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 26-4-2024 03:23 PM , Processed in 0.067132 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表