找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3229|回复: 1
收起左侧

TM4C123简易信号发生器源码

[复制链接]
ID:337321 发表于 2018-5-24 15:09 | 显示全部楼层 |阅读模式
简易信号发生器
1启用定时器中断,从微控制器引脚输出占空比为x%, x kHz的方波。
其中x为你的学号后2位,当后两位为00时,可使用学号倒数4-3位
2 同时在液晶上按如下格式显示:
3 简易信号发生器
4定时器m,引脚n
5Fre=x,Duty=x
6你的姓名

注:在本目录存放整个工程项目,包括源文件、头文件、说明等

单片机源程序如下:
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <string.h>

  6. #include "inc/hw_memmap.h"
  7. #include "inc/hw_types.h"
  8. #include "inc/hw_timer.h"
  9. #include "inc/hw_ints.h"
  10. #include "inc/hw_gpio.h"
  11. #include "inc/hw_i2c.h"
  12. #include "inc/hw_sysctl.h"
  13. #include "driverlib/timer.h"
  14. #include "driverlib/interrupt.h"
  15. #include "driverlib/sysctl.h"
  16. #include "driverlib/systick.h"
  17. #include "driverlib/gpio.h"
  18. #include "driverlib/pin_map.h"
  19. #include "driverlib/ssi.h"
  20. #include "driverlib/i2c.h"
  21. #include "driverlib/udma.h"
  22. #include "driverlib/fpu.h"
  23. #include "driverlib/rom.h"
  24. #include "uc1701.h"

  25. //*****************************************************************************
  26. //Attention
  27. //TM4C123 NMI unlock - To those who want to use PF0 and PD7, be reminded that these pins defaults as NMI ! ! !
  28. //
  29. //*****************************************************************************

  30. //*****************************************************************************
  31. //
  32. // The error routine that is called if the driver library encounters an error.
  33. //
  34. //*****************************************************************************
  35. #ifdef DEBUG
  36. void
  37. __error__(char *pcFilename, unsigned long ulLine)
  38. {
  39. }
  40. #endif

  41. //*****************************************************************************
  42. //
  43. // Global variables
  44. //
  45. //
  46. //
  47. //*****************************************************************************
  48. unsigned char flag;
  49. unsigned int i;
  50. //*****************************************************************************
  51. //
  52. // The interrupt application.  
  53. // Timer interrupt
  54. //
  55. //
  56. //*****************************************************************************
  57. void Timer0IntHandler(void)
  58. {
  59.     // Clear the timer interrupt
  60.     TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);


  61.         if(i<7)
  62.         GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4, 1<<4);  // 绿LED灯:亮;
  63.    
  64.                           else
  65.                           GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4, 0<<4);  // 绿LED灯:亮
  66.                                 
  67.                                 i=i+1;
  68.                                 if(i==60)
  69.                                         i=0;
  70.    
  71. }

  72. //*****************************************************************************
  73. char HZ[]={
  74. 0x08,0x04,0xC3,0x12,0x26,0x8A,0xA2,0xAA,0xA4,0xA3,0x22,0x26,0xEA,0x02,0x02,0x00,
  75. 0x00,0x00,0xFF,0x00,0x00,0x3F,0x24,0x24,0x24,0x3F,0x00,0x80,0xFF,0x00,0x00,0x00,/*"简",0*/
  76. 0x00,0x00,0x00,0x00,0x7F,0xC9,0x49,0x49,0x49,0x49,0x49,0x7F,0x00,0x00,0x00,0x00,
  77. 0x00,0x08,0x04,0x42,0x23,0x12,0x8E,0x42,0x32,0x0E,0x42,0x82,0x42,0x3E,0x00,0x00,/*"易",1*/
  78. 0x00,0x80,0x60,0xF8,0x07,0x00,0x04,0x24,0x24,0x25,0x26,0x24,0x24,0x24,0x04,0x00,
  79. 0x01,0x00,0x00,0xFF,0x00,0x00,0x00,0xF9,0x49,0x49,0x49,0x49,0x49,0xF9,0x00,0x00,/*"信",2*/
  80. 0x80,0x80,0x80,0xBE,0xA2,0xA2,0xA2,0xA2,0xA2,0xA2,0xA2,0xBE,0x80,0x80,0x80,0x00,
  81. 0x00,0x00,0x00,0x06,0x05,0x04,0x04,0x04,0x44,0x84,0x44,0x3C,0x00,0x00,0x00,0x00,/*"号",3*/
  82. 0x00,0x00,0x18,0x16,0x10,0xD0,0xB8,0x97,0x90,0x90,0x90,0x92,0x94,0x10,0x00,0x00,
  83. 0x00,0x20,0x10,0x8C,0x83,0x80,0x41,0x46,0x28,0x10,0x28,0x44,0x43,0x80,0x80,0x00,/*"发",4*/
  84. 0x80,0x40,0x30,0x1E,0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x00,
  85. 0x40,0x40,0x42,0x42,0x42,0x42,0x42,0x7F,0x42,0x42,0x42,0x42,0x42,0x40,0x40,0x00,/*"生",5*/
  86. 0x80,0x80,0x9E,0x92,0x92,0x92,0x9E,0xE0,0x80,0x9E,0xB2,0xD2,0x92,0x9E,0x80,0x00,
  87. 0x08,0x08,0xF4,0x94,0x92,0x92,0xF1,0x00,0x01,0xF2,0x92,0x94,0x94,0xF8,0x08,0x00,/*"器",6*/
  88. };

  89. char HZ1[]={
  90. 0x10,0x0C,0x44,0x44,0x44,0x44,0x45,0xC6,0x44,0x44,0x44,0x44,0x44,0x14,0x0C,0x00,
  91. 0x80,0x40,0x20,0x1E,0x20,0x40,0x40,0x7F,0x44,0x44,0x44,0x44,0x44,0x40,0x40,0x00,/*"定",0*/
  92. 0x00,0xFC,0x84,0x84,0x84,0xFC,0x00,0x10,0x10,0x10,0x10,0x10,0xFF,0x10,0x10,0x00,
  93. 0x00,0x3F,0x10,0x10,0x10,0x3F,0x00,0x00,0x01,0x06,0x40,0x80,0x7F,0x00,0x00,0x00,/*"时",1*/
  94. 0x80,0x80,0x9E,0x92,0x92,0x92,0x9E,0xE0,0x80,0x9E,0xB2,0xD2,0x92,0x9E,0x80,0x00,
  95. 0x08,0x08,0xF4,0x94,0x92,0x92,0xF1,0x00,0x01,0xF2,0x92,0x94,0x94,0xF8,0x08,0x00,/*"器",2*/
  96. };

  97. char HZ2[]={
  98. 0x00,0x02,0xE2,0x22,0x22,0x22,0x22,0x3E,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
  99. 0x00,0x03,0x02,0x02,0x42,0x82,0x42,0x3E,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,/*"引",0*/
  100. 0x00,0xFE,0x22,0x22,0xFE,0x80,0x88,0xFF,0x88,0x80,0x00,0xFC,0x04,0x04,0xFC,0x00,
  101. 0x80,0x7F,0x02,0x82,0xFF,0x10,0x3C,0x13,0x18,0x30,0x00,0xFF,0x04,0x08,0x07,0x00,/*"脚",1*/

  102. };

  103. char HZ3[]={
  104. 0x00,0x04,0x44,0x44,0x44,0x44,0xFC,0x22,0x22,0x22,0x22,0x23,0x02,0x00,0x00,0x00,
  105. 0x04,0x04,0x04,0x04,0x04,0x04,0x7F,0x82,0x82,0x82,0x82,0x82,0x82,0xF2,0x00,0x00,/*"毛",0*/
  106. 0x10,0x0C,0x44,0x54,0x54,0xFC,0x55,0x56,0x54,0xFC,0x54,0x54,0x44,0x14,0x0C,0x00,
  107. 0x11,0x11,0x09,0x05,0x03,0x21,0x25,0x45,0x49,0x91,0x03,0x05,0x09,0x11,0x11,0x00,/*"寒",1*/
  108. 0x00,0x00,0x00,0xBE,0x2A,0x2A,0x2A,0xEA,0x2A,0x2A,0x2A,0x3E,0x00,0x00,0x00,0x00,
  109. 0x00,0x44,0x42,0x49,0x49,0x49,0x49,0x7F,0x49,0x49,0x49,0x49,0x41,0x40,0x00,0x00,/*"星",2*/

  110. };
  111. void Timer1IntHandler(void)
  112. {
  113.         // Clear the timer interrupt
  114.         TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);

  115.     if(GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_6))
  116.     {
  117.         GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_6, 0<<6);  // 红LED灯:亮
  118.     }
  119.     else
  120.     {
  121.         GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_6, 1<<6);  // 红LED灯:灭
  122.     }
  123. }

  124. //*****************************************************************************
  125. //
  126. // The main application.  
  127. // Blink the led
  128. // led2-PA4
  129. //
  130. //*****************************************************************************
  131. int
  132. main(void)
  133. {
  134.     uint32_t ui32Period;
  135.           i=0;
  136.     //
  137.     // The FPU should be enabled because some compilers will use floating-
  138.     // point registers, even for non-floating-point code. If the FPU is not
  139.     // enabled this will cause a fault.  This also ensures that floating-
  140.     // point operations could be added to this application and would work
  141.     // correctly and use the hardware floating-point unit.  Finally, lazy
  142.     // stacking is enabled for interrupt handlers.  This allows floating-
  143.     // point instructions to be used within interrupt handlers, but at the
  144.     // expense of extra stack usage.
  145.     FPUEnable();
  146.     FPULazyStackingEnable();

  147.     //
  148.     // 注:下面为用PLL和不用PLL设置系统时钟的方法
  149.     // 启用 PLL(通过合理设置,最高可到 80MHz),设置系统时钟为 40 MHz
  150.     SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
  151.     // 不启用 PLL(不超过外接晶振频率,本核心板为 16MHz),设置系统时钟为 16 MHz
  152.     //SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
  153.    
  154.     // LED 引脚初始化
  155.     // 设置 PA4 为输出
  156.     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);            // 使能 GPIO A模块
  157.     GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_4);     // PA4 设置为输出
  158.     GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_4,1<<4);          // 绿 LED 灯初始态:灭
  159.     // 设置 PD6 为输出
  160.     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);            // 使能 GPIO D模块
  161.     GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_6);     // PD6 设置为输出
  162.     GPIOPinWrite(GPIO_PORTD_BASE,GPIO_PIN_6,1<<6);          // 红 LED 灯初始态:灭
  163.     //
  164.     // Timer0 init
  165.     SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
  166.     TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
  167.     // Timer1 init
  168.     SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
  169.     TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC);
  170.    
  171.     //
  172.                 UC1701Init(60000);
  173.     UC1701Clear();
  174.                 UC1701ChineseDispaly(0, 1, 7,HZ);
  175.                 UC1701ChineseDispaly(1, 1, 3,HZ1);
  176.                 UC1701CharDispaly(1, 7, "0,");
  177.                 UC1701ChineseDispaly(1, 9, 2,HZ2);
  178.                 UC1701CharDispaly(1, 13, "PA4");
  179.                 UC1701CharDispaly(2, 1, "Fre=12,Duty=12");
  180.                 UC1701ChineseDispaly(3, 1, 3,HZ3);
  181.     // Timer delay period
  182.     ui32Period = (SysCtlClockGet());        // 获取系统运行频率,单位为 Hz
  183.     TimerLoadSet(TIMER0_BASE, TIMER_A, (ui32Period/1200000)-1);    // 设置 Timer0 溢出周期
  184.     TimerLoadSet(TIMER1_BASE, TIMER_A, ui32Period/3 -1);  // 设置 Timer1 溢出周期
  185.     //
  186.     // interrupt enable--timerxA
  187.     IntEnable(INT_TIMER0A);     //Enables a timer interrupt
  188.     //Enables individual timer interrupt sources
  189.     TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);   
  190.     IntEnable(INT_TIMER1A);     //Enables a timer interrupt
  191.     //Enables individual timer interrupt sources
  192.     TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT);   

  193.         IntMasterEnable();  //Enables the processor interrupt

  194.     TimerIntRegister(TIMER0_BASE, TIMER_BOTH, Timer0IntHandler); //  Timer0 中断服务函数入口
  195.     TimerIntRegister(TIMER1_BASE, TIMER_BOTH, Timer1IntHandler); //  Timer1 中断服务函数入口
  196.     //
  197.     // timer0 enable--This will start the timer and interrupts
  198.     TimerEnable(TIMER0_BASE, TIMER_A);      // 使能 Timer0:TIMER_A
  199.     TimerEnable(TIMER1_BASE, TIMER_A);      // 使能 Timer1:TIMER_A
  200.    
  201.     ui32Period = SysCtlClockGet()/40000;    // 【示例】获取系统时钟,并 40k 分频
  202.     SysCtlDelay(ui32Period);                // 【示例】调用系统延时函数
  203.    

  204.     while(1)    // 无限循环(死循环)
  205.     {
  206.         
  207.     }
  208. }
  209. // The end.
复制代码

所有资料51hei提供下载:
s07-lab7B1.rar (1.76 MB, 下载次数: 11)
回复

使用道具 举报

ID:364014 发表于 2018-7-21 18:46 | 显示全部楼层
可以给新手做参考,写的还不错
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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