|
请问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
谢谢。 |
|
|
|
|
|
|
|
发表于 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 就在程序里改。
- #include "16f877a.h"
- #fuses HS,PROTECT,NoWDT,put,brownout
- #use delay(clock=20000000)
- #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
- #define LCD_DATA_PORT getenv("SFR:PORTD")
- #include "LCD-pikachew.C"
- int1 fiA0=0; // Flag
- int1 fiA1=0;
- void main()
- {
- lcd_init();
- lcd_putc("\fCar Park Sys");
- lcd_putc("\nV1.00 ");
- delay_ms(2000);
- lcd_putc("\f"); // clear LCD screen
- fiA0=input(pin_a0); // initialize
- fiA1=input(pin_a1); // initialize
- while(1)
- {
- if(fiA0!=input(pin_a0)) // Test if Flag is change status
- {
- fiA0=input(pin_a0); // lock Flag
- delay_ms(100); // debounce
- if(input(pin_a0))
- {
- output_low(pin_b0);
- printf("No1\n\r");
- lcd_putc("\fone");
- }
- else
- {
- output_high(pin_b0);
- printf("yes1\n\r");
-
- lcd_putc("two");
- }
- }
-
- if(fiA1!=input(pin_a1)) // Test Flag
- {
- fiA1=input(pin_a1); // lock Flag
- delay_ms(100); // debounce
- if(input(pin_a1)==1)
- {
- output_low(pin_b1);
- printf("No2\n\r");
- lcd_putc("\nthree");
- }
- else
- {
- output_high(pin_b1);
- printf("yes2\n\r");
-
- lcd_putc("\nfour");
- }
- }
- }
- }
复制代码
下面是"LCD-pikachew.C"
- // "LCD-pikachew.C"
- struct lcd_pin_map { // This structure is overlayed
- BOOLEAN enable; // on to an I/O port to gain
- BOOLEAN rs; // access to the LCD pins.
- BOOLEAN rw; // The bits are allocated from
- BOOLEAN unused; // low order up. ENABLE will
- int data : 4; // be pin B0.
- } lcd;
- #if defined(__PCH__)
- #if defined use_portb_lcd
- #byte lcd = 0xF81 // This puts the entire structure
- #else
- #byte lcd = 0xF83 // This puts the entire structure
- #endif
- #else
- #if defined use_portb_lcd
- #byte lcd = 6 // on to port B (at address 6)
- #else
- #byte lcd = 8 // on to port D (at address 8)
- #endif
- #endif
- #if defined use_portb_lcd
- #define set_tris_lcd(x) set_tris_b(x)
- #else
- #define set_tris_lcd(x) set_tris_d(x)
- #endif
- #define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines
- #define lcd_line_two 0x40 // LCD RAM address for the second line
- BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
- // These bytes need to be sent to the LCD
- // to start it up.
- // The following are used for setting
- // the I/O port direction register.
- struct lcd_pin_map const LCD_WRITE = {0,0,0,0,0}; // For write mode all pins are out
- struct lcd_pin_map const LCD_READ = {0,0,0,0,15}; // For read mode data pins are in
- BYTE lcd_read_byte() {
- BYTE low,high;
- set_tris_lcd(LCD_READ);
- lcd.rw = 1;
- delay_cycles(1);
- lcd.enable = 1;
- delay_cycles(1);
- high = lcd.data;
- lcd.enable = 0;
- delay_cycles(1);
- lcd.enable = 1;
- delay_us(1);
- low = lcd.data;
- lcd.enable = 0;
- set_tris_lcd(LCD_WRITE);
- return( (high<<4) | low);
- }
- void lcd_send_nibble( BYTE n ) {
- lcd.data = n;
- delay_cycles(1);
- lcd.enable = 1;
- delay_us(2);
- lcd.enable = 0;
- }
- void lcd_send_byte( BYTE address, BYTE n ) {
- lcd.rs = 0;
- while ( bit_test(lcd_read_byte(),7) ) ;
- lcd.rs = address;
- delay_cycles(1);
- lcd.rw = 0;
- delay_cycles(1);
- lcd.enable = 0;
- lcd_send_nibble(n >> 4);
- lcd_send_nibble(n & 0xf);
- }
- void lcd_init() {
- BYTE i;
- set_tris_lcd(LCD_WRITE);
- lcd.rs = 0;
- lcd.rw = 0;
- lcd.enable = 0;
- delay_ms(15);
- for(i=1;i<=3;++i) {
- lcd_send_nibble(3);
- delay_ms(5);
- }
- lcd_send_nibble(2);
- for(i=0;i<=3;++i)
- lcd_send_byte(0,LCD_INIT_STRING[i]);
- }
- void lcd_gotoxy( BYTE x, BYTE y) {
- BYTE address;
- if(y!=1)
- address=lcd_line_two;
- else
- address=0;
- address+=x-1;
- lcd_send_byte(0,0x80|address);
- }
- void lcd_putc( char c) {
- switch (c) {
- case '\f' : lcd_send_byte(0,1);
- delay_ms(2);
- break;
- case '\n' : lcd_gotoxy(1,2); break;
- case '\b' : lcd_send_byte(0,0x10); break;
- default : lcd_send_byte(1,c); break;
- }
- }
- char lcd_getc( BYTE x, BYTE y) {
- char value;
- lcd_gotoxy(x,y);
- while ( bit_test(lcd_read_byte(),7) ); // wait until busy flag is low
- lcd.rs=1;
- value = lcd_read_byte();
- lcd.rs=0;
- return(value);
- }
复制代码 |
|
|
|
|
|
|
|
楼主 |
发表于 15-10-2011 07:14 PM
|
显示全部楼层
那么, 如果你是按你提供的电路的话, 那么你的电路错了。
你的LDR 的电, 不可以通过PortB 来供电, 应 ...
pic 发表于 13-10-2011 11:43 PM
- if((input(pin_a0)==1) && (input(pin_a1)==1))
- {
- delay_ms(1000);
- lcd_putc("\fPL avialable:0");
- printF("PL avialable:0");
- }
- if((input(pin_a0)==0) && (input(pin_a1)==0))
- {
- delay_ms(1000);
- lcd_putc("\fPL available:2");
- printf("PL available:2");
- delay_ms(2000);
- lcd_putc("\fPL1-empty");
- lcd_putc("\nPL2-empty");
-
- }
-
- if((input(pin_a0)==1) || (input(pin_a1)==1))
- {
- delay_ms(1000);
- lcd_putc("\nPL available:1/2 ");
- printf("PL available:1/2 ");
-
- }
-
- }
复制代码
现在面对一些问题
就是是否在这里也要加入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的部分写到不好。。 |
|
|
|
|
|
|
|
楼主 |
发表于 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
|
显示全部楼层
- if((input(pin_a0)==1) && (input(pin_a1)==1) && (input(pin_a2)==1))
- {
- delay_ms(1000);
- lcd_putc("\fPL avialable:0");
-
- }
- else if((input(pin_a0)==0) && (input(pin_a1)==0) && (input(pin_a2)==0))
- {
- delay_ms(1000);
- lcd_putc("\fPL available:3");
- delay_ms(2000);
- lcd_putc("\fPL1-empty");
- lcd_putc("\nPL2-empty");
- delay_ms(1000);
- lcd_putc("\fPL3-empty");
- delay_ms(1000);
-
- }
-
- else if((input(pin_a0)==1) || (input(pin_a1)==1) || (input(pin_a2)==1))
- {
-
- lcd_putc("\nPL available:2/3");
-
-
- }
-
- else if((input(pin_a0)==0) && (input(pin_a1)==1) && (input(pin_a2)==0))
- {
-
- lcd_putc("\nPL available:2/3");
-
-
- }
-
- else if((input(pin_a0)==0) && (input(pin_a1)==1) && (input(pin_a2)==1))
- {
-
- lcd_putc("\nPL available:1/3");
-
-
- }
-
- else if((input(pin_a0)==1) && (input(pin_a1)==0) && (input(pin_a2)==0))
- {
-
- lcd_putc("\nPL available:2/3");
-
-
- }
- else if((input(pin_a0)==1) && (input(pin_a1)==0) && (input(pin_a2)==1))
- {
-
- lcd_putc("\nPL available:1/3");
-
-
- }
- else if((input(pin_a0)==1) && (input(pin_a1)==1) && (input(pin_a2)==0))
- {
-
- lcd_putc("\nPL available:1/3");
-
-
- }
复制代码
小弟想问一下,请问以上的CODE可以被SIMPLIFIED 吗?
比如 不用打那么多个CONDITIONS.
谢谢。 |
|
|
|
|
|
|
|
发表于 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”。
当然可以, 这时就是你考虑你的程序结构性的问题了。。
假设是100个, 1000个泊车位呢?
pic 发表于 24-10-2011 10:42 AM |
|
|
|
|
|
|
|
发表于 25-10-2011 11:34 AM
|
显示全部楼层
回复 112# pikachew
快速看, 看不懂你的code, 除非花时间去慢慢看
- counter = (input(pin_a0) + input(pin_a1) + input(pin_a2));
复制代码
上面不成立。。
考虑改成
- counter=0;
- if(input(pin_a0)) counter++;
- if(input(pin_a1)) counter++;
- if(input(pin_a2)) counter++;
复制代码 |
|
|
|
|
|
|
|
楼主 |
发表于 25-10-2011 06:32 PM
|
显示全部楼层
回复 pikachew
快速看, 看不懂你的code, 除非花时间去慢慢看
上面不成立。。
考虑改成
pic 发表于 25-10-2011 11:34 AM
eg.
- if (counter =0)
- {
- lcd_putc("\fPL available:",((3-counter)/3));
- }
复制代码
现在想不通为什么书本里可以这样放可是在这里就不能这样放。。。
就算加了" ) " 它还是一直有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
哦哦。明白了。
- ' Header******************************************************
- program example_11 ' Program name
- dim i as byte ' Variable is of byte type
- main: ' Start of program
- UART1_Init(19200) ' Initialize USART module
- ' (8 bit, 19200 baud rate, no parity bit...)
- while 1 ' Endless loop
- if UART1_Data_Ready() then ' If data has been received
- i = UART1_Read() ' read it
- UART1_Write(i) ' and send it back
- end if
- wend
- 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/ |
|
|
|
|
|
|
|
发表于 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 , 是用
- #use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)
复制代码
UART1_Data_Ready()
当USART有data 来了。。
在CCS C , 是用
|
|
|
|
|
|
|
|
楼主 |
发表于 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里的?
我试了这但是不能走,请问有谁会呢?
- if ( getchar())
- {
- printf("ABCD");
- }
- }
复制代码
谢谢。 |
|
|
|
|
|
|
|
发表于 31-10-2011 10:01 AM
|
显示全部楼层
回复 118# pikachew
- if ( getchar())
- {
- printf("ABCD");
- }
- }
复制代码
“getchar()” 这个功能在电脑C语言是无限等待键盘一字节(byte)输入。 也就是说如果没等到键盘回传值,是不会执行下一行程序的。
在MCU 这个指令被改用在 “UART”里做无限单一字节接收,用了它MCU就不用在做其它的工作了。
“getchar()”只出现在一些“MCU UART”入门教程里。
- ' Header******************************************************
- program example_11 ' Program name
- dim i as byte ' Variable is of byte type
- main: ' Start of program
- UART1_Init(19200) ' Initialize USART module
- ' (8 bit, 19200 baud rate, no parity bit...)
- while 1 ' Endless loop
- if UART1_Data_Ready() then ' If data has been received
- i = UART1_Read() ' read it
- UART1_Write(i) ' and send it back
- end if
- wend
- end. ' End of program
复制代码
这个版主已经解释很清楚了,我不在多说。
只是补充一点的是所有MCU的UART 接收和发送资料的缓冲区就只是一个字节(byte)而已。如果来不及处理接收缓冲区的资料,就有可能被下一个接收到的新资料给覆盖掉(资料丢失)。
- printf("\nPARKING LOT3-OCCUPIED\r");
复制代码
这个MCU发送资料给电脑function,一旦执行,也就等待发送完所有资料才会执行下一行程序。有点类似“getchar()”。
如果在高效率的要求底下,就只能运用MCU本身的中断服务(Interrupt)功能来解决了。以上的所提到function 只会降低MCU的运行效果。 |
|
|
|
|
|
|
|
发表于 31-10-2011 04:46 PM
|
显示全部楼层
|
|
|
|
|
|
| |
本周最热论坛帖子
|