2013年10月22日 星期二

PIC LCD顯示 1-100 簡易範例


  1. #include <p18f4520.h> //納入微控制器定義檔
  2. #include <delays.h> //納入時間延遲函式庫定義檔
  3. #include "evm_lcd.h" //納入LCD函式庫定義檔
  4. #include    <stdio.h>

  5. // 設定微處理器相關結構位元
  6. #pragma config OSC=HS, BOREN=OFF, BORV = 2, PWRT=ON, WDT=OFF, LVP=OFF 

  7. // 定義時序頻率
  8. #define OSC_CLOCK 10
  9. // 宣告時間延遲函式原型
  10. void delay_ms(long A);

  11. void main() {

  12. int i;
  13.    OpenLCD(); // 初始化LCD模組
  14.    WriteCmdLCD( 0x01 ); // 清除LCD顯示資料
  15.    LCD_Set_Cursor( 0, 0 ); // 顯示位置回至第0行第0格
  16.    delay_ms(1); // 時間延遲
  17.    putrsLCD("Welcome to PIC"); // 顯示資料
  18.    LCD_Set_Cursor( 1, 0 ); // 顯示位置調至第1行第0格
  19.    delay_ms(1); // 時間延遲
  20.    putrsLCD("Micro-Controller");// 顯示資料
  21. delay_ms(1000);
  22.     WriteCmdLCD( 0x01 ); // 清除LCD顯示資料
  23.    for (i=0;i<100;i++)
  24. {
  25. char buf[10];
  26.     //WriteCmdLCD( 0x01 );
  27.     LCD_Set_Cursor( 0, 0 );
  28.     delay_ms(1000);
  29.     sprintf(buf,"%d",i);
  30.     putsLCD(buf);
  31. }
  32.    Sleep(); // 進入睡眠省電模式
  33.    while(1); // 永久迴圈
  34. }

  35. // 時間延遲函式
  36. void delay_ms(long A) {
  37. //This function is only good for OSC_CLOCK higher than 4MHz
  38. long i;
  39. int us2TCY;
  40. us2TCY=(10*OSC_CLOCK)>>2;
  41. for(i=0;i<A;i++) Delay100TCYx(us2TCY);
  42. }


沒有留言:

張貼留言