找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3448|回复: 0
收起左侧

MC9S12XS128并口12864液晶显示程序,包含按键中断,按键可翻动的程序源码

[复制链接]
ID:242450 发表于 2017-10-24 15:19 | 显示全部楼层 |阅读模式
程序在12864液晶上显示一首词,按键可翻动词句。
0.png
单片机源程序如下:
  1. /*---------------------------------------------------------*/
  2. /************************************************************
  3. 飞翔科技MC9S12XS128汽车电子开发板
  4. ************************************************************/
  5. /*---------------------------------------------------------*/
  6. #include <hidef.h>      /* common defines and macros */
  7. #include "derivative.h"      /* derivative-specific definitions */
  8. #include "LCD.h"      

  9. #define LEDCPU PORTK_PK4
  10. #define LEDCPU_dir DDRK_DDRK4

  11. #define KEY1 PTIH_PTIH3
  12. #define KEY2 PTIH_PTIH2
  13. #define KEY1_dir DDRH_DDRH3
  14. #define KEY2_dir DDRH_DDRH2

  15. #define  BUS_CLOCK                   32000000           //总线频率
  16. #define  OSC_CLOCK                   16000000           //晶振频率

  17. char *poem[11] = {
  18. "长征",
  19. "毛泽东",
  20. "红军不怕远征难,",
  21. "万水千山只等闲,",
  22. "五岭逶迤腾细浪,",
  23. "乌蒙磅礴走泥丸。",
  24. "金沙水拍云崖暖,",
  25. "大渡桥横铁索寒,",
  26. "更喜岷山千里雪,",
  27. "三军过后尽开颜!",
  28. "",
  29. } ;

  30. unsigned char single = 0;    //液晶翻页的标志符


  31. /*************************************************************/
  32. /*                      初始化锁相环                         */
  33. /*************************************************************/
  34. void INIT_PLL(void)
  35. {
  36.     CLKSEL &= 0x7f;       //set OSCCLK as sysclk
  37.     PLLCTL &= 0x8F;       //Disable PLL circuit
  38.     CRGINT &= 0xDF;
  39.    
  40.     #if(BUS_CLOCK == 40000000)
  41.       SYNR = 0x44;
  42.     #elif(BUS_CLOCK == 32000000)
  43.       SYNR = 0x43;     
  44.     #elif(BUS_CLOCK == 24000000)
  45.       SYNR = 0x42;
  46.     #endif

  47.     REFDV = 0x81;         //PLLCLK=2×OSCCLK×(SYNDIV+1)/(REFDIV+1)=64MHz ,fbus=32M
  48.     PLLCTL =PLLCTL|0x70;  //Enable PLL circuit
  49.     asm NOP;
  50.     asm NOP;
  51.     while(!(CRGFLG&0x08)); //PLLCLK is Locked already
  52.     CLKSEL |= 0x80;        //set PLLCLK as sysclk
  53. }

  54. /************************************************************/
  55. /*                    初始化ECT模块                         */
  56. /************************************************************/
  57. void initialize_ect(void){
  58.   TSCR1_TFFCA = 1;  // 定时器标志位快速清除
  59.   TSCR1_TEN = 1;    // 定时器使能位. 1=允许定时器正常工作; 0=使主定时器不起作用(包括计数器)
  60.   TIOS  = 0xff;      //指定所有通道为输出比较方式
  61.   TCTL1 = 0x00;            // 后四个通道设置为定时器与输出引脚断开
  62.   TCTL2 = 0x00;     // 前四个通道设置为定时器与输出引脚断开
  63.   TIE   = 0x00;     // 禁止所有通道定时中断
  64.   TSCR2 = 0x07;            // 预分频系数pr2-pr0:111,,时钟周期为4us,
  65.   TFLG1 = 0xff;            // 清除各IC/OC中断标志位
  66.   TFLG2 = 0xff;     // 清除自由定时器中断标志位
  67. }

  68. /*************************************************************/
  69. /*                       初始化按键                          */
  70. /*************************************************************/
  71. void init_key(void)
  72. {
  73.      KEY1_dir =0;       //设置为输入
  74.      KEY2_dir=0;
  75.      PPSH = 0x00;                      //极性选择寄存器,选择下降沿;
  76.      PIFH = 0x0C;                                        //对PIFH的每一位写1来清除标志位;
  77.      PIEH = 0x0C;                      //中断使能寄存器;
  78. }


  79. /*************************************************************/
  80. /*                    按键中断函数                           */
  81. /*************************************************************/
  82. #pragma CODE_SEG __NEAR_SEG NON_BANKED
  83. interrupt void PTH_inter(void)
  84. {
  85.    if(PIFH != 0)     //判断中断标志
  86.    {
  87.       PIFH = 0xff;     //清除中断标志
  88.       if(KEY2 == 0)
  89.       {
  90.          delay1ms(5);
  91.          if(KEY2 == 0)
  92.          {
  93.            if(single == 0)
  94.             single = 10;
  95.            else single-=1;
  96.            
  97.          }
  98.       }


  99.       if(KEY1 == 0)
  100.       {
  101.          delay1ms(5);
  102.          if(KEY1 == 0)
  103.          {
  104.            if(single == 10)
  105.             single = 0;
  106.            else single+=1;

  107.          }
  108.       
  109.       }
  110.       
  111.      lcd_clear();
  112.      if(single == 0) {
  113.        lcd_string(0,2,poem[0]);
  114.        lcd_string(1,3,poem[1]);
  115.        lcd_string(2,0,poem[2]);
  116.        lcd_string(3,0,poem[3]);
  117.      }
  118.      else if(single == 1) {
  119.        lcd_string(0,3,poem[1]);
  120.        lcd_string(1,0,poem[2]);
  121.        lcd_string(2,0,poem[3]);
  122.        lcd_string(3,0,poem[4]);
  123.      }
  124.      else if(single == 2) {
  125.        lcd_string(0,0,poem[2]);
  126.        lcd_string(1,0,poem[3]);
  127.        lcd_string(2,0,poem[4]);
  128.        lcd_string(3,0,poem[5]);
  129.      }
  130.      else if(single == 8) {
  131.        lcd_string(0,0,poem[8]);
  132.        lcd_string(1,0,poem[9]);
  133.        lcd_string(2,0,poem[10]);
  134.        lcd_string(3,2,poem[0]);
  135.      }
  136.      else if(single == 9) {
  137.        lcd_string(0,0,poem[9]);
  138.        lcd_string(1,0,poem[10]);
  139.        lcd_string(2,2,poem[0]);
  140.        lcd_string(3,3,poem[1]);
  141.      }
  142.      else if(single == 10) {
  143.        lcd_string(0,0,poem[10]);
  144.        lcd_string(1,2,poem[0]);
  145.        lcd_string(2,3,poem[1]);
  146.        lcd_string(3,0,poem[2]);
  147.      }
  148.      else {
  149.        lcd_string(0,0,poem[single]);
  150.        lcd_string(1,0,poem[single + 1]);
  151.        lcd_string(2,0,poem[single + 2]);
  152.        lcd_string(3,0,poem[single + 3]);
  153.      }
  154.    }
  155. }
  156. #pragma CODE_SEG DEFAULT


  157. /*************************************************************/
  158. /*                         主函数                            */
  159. ……………………

  160. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

完整代码下载:
20并口液晶.rar (238.55 KB, 下载次数: 28)
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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