找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4394|回复: 7
打印 上一主题 下一主题
收起左侧

stm32交通灯程序设计

  [复制链接]
跳转到指定楼层
楼主
ID:464849 发表于 2019-1-8 20:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #include "stm32f10x.h"
  2. #include "delay.h"

  3. void Delay(u16 nCount);
  4. void GPIO_Config(void)
  5. {
  6.          GPIO_InitTypeDef        GPIO_InitStructure;
  7.         
  8.   GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
  9.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  10.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  11.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  12.         

  13.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
  14.   GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  15.   GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  16.   GPIO_Init(GPIOA,&GPIO_InitStructure);
  17.         
  18.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  19.   GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  20.   GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
  21.   GPIO_Init(GPIOA,&GPIO_InitStructure);
  22. }
  23.   
  24.         void SPI_Config(void)
  25.         {
  26.         SPI_InitTypeDef  SPI1_InitStructure;
  27.         SPI1_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;//设置SPI单向或者双向的数据模式:SPI设置为双线模式
  28.         SPI1_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_64;//定义波特率预分频的值:波特率预分频值为64
  29.         SPI1_InitStructure.SPI_DataSize=SPI_DataSize_8b;//设置SPI的数据大小:SPI发送接收8位帧结构
  30.         SPI1_InitStructure.SPI_Mode=SPI_Mode_Master;//设置SPI工作模式,设置为主模式
  31.         SPI1_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB; //高位在先
  32.         SPI1_InitStructure.SPI_CPOL = SPI_CPOL_High;//串行同步时钟的空闲状态为高电平                                       
  33.         SPI1_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //串行同步时钟的第二个跳变沿(上升或下降)数据被采样
  34.         SPI1_InitStructure.SPI_NSS = SPI_NSS_Soft; // 使用软件模式
  35.         SPI1_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式
  36.         SPI_I2S_DeInit(SPI1);
  37.         SPI_Init(SPI1, &SPI1_InitStructure); //初始化
  38.   SPI_NSSInternalSoftwareConfig(SPI1, SPI_NSSInternalSoft_Set);//内部制高电平
  39.         SPI_Cmd(SPI1, ENABLE);  
  40.         }
  41.         
  42.         void Write_Max7219(u8 address,u8 dat)
  43. {
  44.   //功能:向MAX7219写入数据
  45.   //入口参数:address、dat
  46.   //出口参数:无
  47.   //说明:
  48.                         GPIO_ResetBits(GPIOA, GPIO_Pin_3);        
  49.       SPI_I2S_SendData(SPI1, address);  //写入地址,即数码管编号
  50.             delay_us(10);
  51.             SPI_I2S_SendData(SPI1, dat);            //写入数据,即数码管显示数字
  52.             delay_us(10);
  53.                         GPIO_SetBits(GPIOA, GPIO_Pin_3);
  54. }

  55. void Init_MAX7219(void)
  56. {
  57. Write_Max7219(0x09, 0xff);       //译码方式:BCD码
  58. Write_Max7219(0x0a, 0x05);       //亮度
  59. Write_Max7219(0x0b, 0x07);       //扫描界限8个数码管显示
  60. Write_Max7219(0x0c, 0x01);       //掉电模式:0,普通模式:1
  61. Write_Max7219(0x0f, 0x00);       //显示测试:1;测试结束,正常显示:0
  62. }

  63. int main(void)
  64. {
  65.         
  66.         
  67.         
  68.         
  69.         
  70.           RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA|RCC_APB2Periph_SPI1, ENABLE);
  71.           RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  72.           delay_init();
  73.                 GPIO_Config();
  74.                 SPI_Config();
  75.           Init_MAX7219();
  76.         
  77.         GPIO_SetBits(GPIOA, GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10);
  78.         
  79.         

  80.         
  81.                 while(1)
  82.         {
  83. GPIO_SetBits(GPIOA, GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10);
  84.                 GPIO_ResetBits(GPIOA,GPIO_Pin_10|GPIO_Pin_1);
  85.                  
  86.                
  87.                 Write_Max7219(0x01, 0x04);
  88.            Write_Max7219(0x02, 0x01);
  89.                 Write_Max7219(0x03, 0x0f);
  90.            Write_Max7219(0x04, 0x0f);
  91.      Write_Max7219(0x05, 0x0f);
  92.            Write_Max7219(0x06, 0x0f);
  93.                 Write_Max7219(0x07, 0x01);
  94.            Write_Max7219(0x08, 0x01);
  95.             delay_ms(1000);
  96.                  Write_Max7219(0x01, 0x03);
  97.            Write_Max7219(0x02, 0x01);
  98.                  Write_Max7219(0x07, 0x00);
  99.            Write_Max7219(0x08, 0x01);
  100.                 Write_Max7219(0x03, 0x0f);
  101.            Write_Max7219(0x04, 0x0f);
  102.      Write_Max7219(0x05, 0x0f);
  103.            Write_Max7219(0x06, 0x0f);
  104.           delay_ms(1000);
  105.                  Write_Max7219(0x01, 0x02);
  106.            Write_Max7219(0x02, 0x01);
  107.                 Write_Max7219(0x03, 0x09);
  108.            Write_Max7219(0x04, 0x00);
  109.                  Write_Max7219(0x03, 0x0f);
  110.            Write_Max7219(0x04, 0x0f);
  111.      Write_Max7219(0x05, 0x0f);
  112.            Write_Max7219(0x06, 0x0f);
  113.                  delay_ms(1000);
  114.                  Write_Max7219(0x01, 0x01);
  115.            Write_Max7219(0x02, 0x01);
  116.                  Write_Max7219(0x07, 0x08);
  117.            Write_Max7219(0x08, 0x00);
  118.                  Write_Max7219(0x03, 0x0f);
  119.            Write_Max7219(0x04, 0x0f);
  120.      Write_Max7219(0x05, 0x0f);
  121.            Write_Max7219(0x06, 0x0f);
  122.                   delay_ms(1000);
  123.                  Write_Max7219(0x01, 0x00);
  124.            Write_Max7219(0x02, 0x01);
  125.           Write_Max7219(0x07, 0x07);
  126.            Write_Max7219(0x08, 0x00);
  127.                  Write_Max7219(0x03, 0x0f);
  128.            Write_Max7219(0x04, 0x0f);
  129.      Write_Max7219(0x05, 0x0f);
  130.            Write_Max7219(0x06, 0x0f);
  131.                  delay_ms(1000);
  132.                  Write_Max7219(0x01, 0x09);
  133.            Write_Max7219(0x02, 0x00);
  134.                 Write_Max7219(0x07, 0x06);
  135.            Write_Max7219(0x08, 0x00);
  136.                  Write_Max7219(0x03, 0x0f);
  137.            Write_Max7219(0x04, 0x0f);
  138.      Write_Max7219(0x05, 0x0f);
  139.            Write_Max7219(0x06, 0x0f);
  140.                  delay_ms(1000);
  141.                  Write_Max7219(0x01, 0x08);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
  142.            Write_Max7219(0x02, 0x00);
  143.                 Write_Max7219(0x07, 0x05);
  144.            Write_Max7219(0x08, 0x00);
  145.                  Write_Max7219(0x03, 0x0f);
  146.            Write_Max7219(0x04, 0x0f);
  147.      Write_Max7219(0x05, 0x0f);
  148.            Write_Max7219(0x06, 0x0f);
  149.                  delay_ms(1000);
  150.                  Write_Max7219(0x01, 0x07);
  151.            Write_Max7219(0x02, 0x00);
  152.                 Write_Max7219(0x07, 0x04);
  153.            Write_Max7219(0x08, 0x00);
  154.                  Write_Max7219(0x03, 0x0f);
  155.            Write_Max7219(0x04, 0x0f);
  156.      Write_Max7219(0x05, 0x0f);
  157.            Write_Max7219(0x06, 0x0f);
  158.                  delay_ms(1000);
  159.                  Write_Max7219(0x01, 0x06);
  160.            Write_Max7219(0x02, 0x00);
  161.                  Write_Max7219(0x07, 0x03);
  162.            Write_Max7219(0x08, 0x00);
  163.                  Write_Max7219(0x03, 0x0f);
  164.            Write_Max7219(0x04, 0x0f);
  165.      Write_Max7219(0x05, 0x0f);
  166.            Write_Max7219(0x06, 0x0f);
  167.                  delay_ms(1000);
  168.                  Write_Max7219(0x01, 0x05);
  169.            Write_Max7219(0x02, 0x00);
  170.                  Write_Max7219(0x07, 0x02);
  171.            Write_Max7219(0x08, 0x00);
  172.                  Write_Max7219(0x03, 0x0f);
  173.            Write_Max7219(0x04, 0x0f);
  174.      Write_Max7219(0x05, 0x0f);
  175.            Write_Max7219(0x06, 0x0f);
  176.                  delay_ms(1000);
  177.                  Write_Max7219(0x01, 0x04);
  178.            Write_Max7219(0x02, 0x00);
  179.                  Write_Max7219(0x07, 0x01);
  180.            Write_Max7219(0x08, 0x00);
  181.                  Write_Max7219(0x03, 0x0f);
  182.            Write_Max7219(0x04, 0x0f);
  183.      Write_Max7219(0x05, 0x0f);
  184.            Write_Max7219(0x06, 0x0f);
  185.                  delay_ms(1000);
  186. GPIO_SetBits(GPIOA, GPIO_Pin_10);
  187.          
  188.                  
  189.                  
  190.                  GPIO_ResetBits(GPIOA, GPIO_Pin_9);
  191.                  Write_Max7219(0x01, 0x03);
  192.            Write_Max7219(0x02, 0x00);
  193.                  Write_Max7219(0x07, 0x03);
  194.            Write_Max7219(0x08, 0x00);
  195.                  Write_Max7219(0x03, 0x0f);
  196.            Write_Max7219(0x04, 0x0f);
  197.      Write_Max7219(0x05, 0x0f);
  198.            Write_Max7219(0x06, 0x0f);
  199.                  delay_ms(1000);
  200.                  Write_Max7219(0x01, 0x02);
  201.            Write_Max7219(0x02, 0x00);
  202.                  Write_Max7219(0x07, 0x02);
  203.            Write_Max7219(0x08, 0x00);
  204.                  Write_Max7219(0x03, 0x0f);
  205.            Write_Max7219(0x04, 0x0f);
  206.      Write_Max7219(0x05, 0x0f);
  207.            Write_Max7219(0x06, 0x0f);
  208.            delay_ms(1000);
  209.                  Write_Max7219(0x01, 0x01);
  210.            Write_Max7219(0x02, 0x00);
  211.                  Write_Max7219(0x07, 0x01);
  212.            Write_Max7219(0x08, 0x00);
  213.                  Write_Max7219(0x03, 0x0f);
  214.            Write_Max7219(0x04, 0x0f);
  215.      Write_Max7219(0x05, 0x0f);
  216.            Write_Max7219(0x06, 0x0f);
  217.            delay_ms(1000);
  218.           GPIO_SetBits(GPIOA, GPIO_Pin_9|GPIO_Pin_1);
  219.                
  220.                  GPIO_ResetBits(GPIOA, GPIO_Pin_8|GPIO_Pin_2);
  221.                 Write_Max7219(0x01, 0x02);
  222.            Write_Max7219(0x02, 0x01);
  223.            Write_Max7219(0x07, 0x05);
  224.            Write_Max7219(0x08, 0x01);
  225.                  Write_Max7219(0x03, 0x0f);
  226.            Write_Max7219(0x04, 0x0f);
  227.      Write_Max7219(0x05, 0x0f);
  228.            Write_Max7219(0x06, 0x0f);
  229.            delay_ms(1000);
  230.                 Write_Max7219(0x01, 0x01);
  231.            Write_Max7219(0x02, 0x01);
  232.            Write_Max7219(0x07, 0x04);
  233.            Write_Max7219(0x08, 0x01);
  234.                  Write_Max7219(0x03, 0x0f);
  235.            Write_Max7219(0x04, 0x0f);
  236.      Write_Max7219(0x05, 0x0f);
  237.            Write_Max7219(0x06, 0x0f);
  238.             delay_ms(1000);
  239.                  Write_Max7219(0x01, 0x00);
  240.            Write_Max7219(0x02, 0x01);
  241.                  Write_Max7219(0x07, 0x03);
  242.            Write_Max7219(0x08, 0x01);
  243.                  Write_Max7219(0x03, 0x0f);
  244.            Write_Max7219(0x04, 0x0f);
  245.      Write_Max7219(0x05, 0x0f);
  246.            Write_Max7219(0x06, 0x0f);
  247.           delay_ms(1000);
  248.                  Write_Max7219(0x01, 0x09);
  249.            Write_Max7219(0x02, 0x00);
  250.                 Write_Max7219(0x07, 0x02);
  251.            Write_Max7219(0x08, 0x01);
  252.                  Write_Max7219(0x03, 0x0f);
  253.            Write_Max7219(0x04, 0x0f);
  254.      Write_Max7219(0x05, 0x0f);
  255.            Write_Max7219(0x06, 0x0f);
  256.                  delay_ms(1000);
  257.                  Write_Max7219(0x01, 0x08);
  258.            Write_Max7219(0x02, 0x00);
  259.                  Write_Max7219(0x07, 0x01);
  260.            Write_Max7219(0x08, 0x01);
  261.                  Write_Max7219(0x03, 0x0f);
  262.            Write_Max7219(0x04, 0x0f);
  263.      Write_Max7219(0x05, 0x0f);
  264.            Write_Max7219(0x06, 0x0f);
  265.                   delay_ms(1000);
  266.                  Write_Max7219(0x01, 0x07);
  267.            Write_Max7219(0x02, 0x00);
  268.                 Write_Max7219(0x03, 0x00);
  269.            Write_Max7219(0x04, 0x01);
  270.                  Write_Max7219(0x03, 0x0f);
  271.            Write_Max7219(0x04, 0x0f);
  272.      Write_Max7219(0x05, 0x0f);
  273.            Write_Max7219(0x06, 0x0f);
  274.                  delay_ms(1000);
  275.                  Write_Max7219(0x01, 0x06);
  276.            Write_Max7219(0x02, 0x00);
  277.                 Write_Max7219(0x07, 0x09);
  278.            Write_Max7219(0x08, 0x00);
  279.                  Write_Max7219(0x03, 0x0f);
  280.            Write_Max7219(0x04, 0x0f);
  281.      Write_Max7219(0x05, 0x0f);
  282.            Write_Max7219(0x06, 0x0f);
  283.                  delay_ms(1000);
  284.                  Write_Max7219(0x01, 0x05);
  285.            Write_Max7219(0x02, 0x00);
  286.                 Write_Max7219(0x07, 0x08);
  287.            Write_Max7219(0x08, 0x00);
  288.                  Write_Max7219(0x03, 0x0f);
  289.            Write_Max7219(0x04, 0x0f);
  290.      Write_Max7219(0x05, 0x0f);
  291.            Write_Max7219(0x06, 0x0f);
  292.                  delay_ms(1000);
  293.                  Write_Max7219(0x01, 0x04);
  294.            Write_Max7219(0x02, 0x00);
  295.                 Write_Max7219(0x03, 0x07);
  296.            Write_Max7219(0x04, 0x00);
  297.                  Write_Max7219(0x03, 0x0f);
  298.            Write_Max7219(0x04, 0x0f);
  299.      Write_Max7219(0x05, 0x0f);
  300.            Write_Max7219(0x06, 0x0f);
  301.                  delay_ms(1000);
  302.                  Write_Max7219(0x01, 0x03);
  303.            Write_Max7219(0x02, 0x00);
  304.                 Write_Max7219(0x07, 0x06);
  305.            Write_Max7219(0x08, 0x00);
  306.                  Write_Max7219(0x03, 0x0f);
  307.            Write_Max7219(0x04, 0x0f);
  308.      Write_Max7219(0x05, 0x0f);
  309.            Write_Max7219(0x06, 0x0f);
  310.                  delay_ms(1000);
  311.                  Write_Max7219(0x01, 0x02);
  312.            Write_Max7219(0x02, 0x00);
  313.                 Write_Max7219(0x07, 0x05);
  314.            Write_Max7219(0x08, 0x00);
  315.                  Write_Max7219(0x03, 0x0f);
  316.            Write_Max7219(0x04, 0x0f);
  317.      Write_Max7219(0x05, 0x0f);
  318.            Write_Max7219(0x06, 0x0f);
  319.                  delay_ms(1000);
  320.                  Write_Max7219(0x01, 0x01);
  321.            Write_Max7219(0x02, 0x00);
  322.                  Write_Max7219(0x07, 0x04);
  323.            Write_Max7219(0x08, 0x00);
  324.                  Write_Max7219(0x03, 0x0f);
  325.            Write_Max7219(0x04, 0x0f);
  326.      Write_Max7219(0x05, 0x0f);
  327.            Write_Max7219(0x06, 0x0f);
  328.                  delay_ms(1000);
  329.                  
  330.           GPIO_SetBits(GPIOA, GPIO_Pin_2);
  331.                  
  332.                 GPIO_ResetBits(GPIOA, GPIO_Pin_0);
  333.                 Write_Max7219(0x01, 0x03);
  334.            Write_Max7219(0x02, 0x00);
  335.                  Write_Max7219(0x07, 0x03);
  336.            Write_Max7219(0x08, 0x00);
  337.                  Write_Max7219(0x03, 0x0f);
  338.            Write_Max7219(0x04, 0x0f);
  339.      Write_Max7219(0x05, 0x0f);
  340.            Write_Max7219(0x06, 0x0f);
  341.                  delay_ms(1000);
  342.                  Write_Max7219(0x01, 0x02);
  343.            Write_Max7219(0x02, 0x00);
  344.                  Write_Max7219(0x07, 0x02);
  345.            Write_Max7219(0x08, 0x00);
  346.                  Write_Max7219(0x03, 0x0f);
  347.            Write_Max7219(0x04, 0x0f);
  348.      Write_Max7219(0x05, 0x0f);
  349.            Write_Max7219(0x06, 0x0f);
  350.            delay_ms(1000);
  351.                  Write_Max7219(0x01, 0x01);
  352.            Write_Max7219(0x02, 0x00);
  353.                  Write_Max7219(0x07, 0x01);
  354.            Write_Max7219(0x08, 0x00);
  355.                  Write_Max7219(0x03, 0x0f);
  356.            Write_Max7219(0x04, 0x0f);
  357.      Write_Max7219(0x05, 0x0f);
  358.            Write_Max7219(0x06, 0x0f);
  359.            delay_ms(1000);
  360.                  
  361.            GPIO_SetBits(GPIOA, GPIO_Pin_4|GPIO_Pin_0);

  362.         }
  363. }
  364. void Delay(u16 nCount)
  365. {
  366.   for(; nCount != 0; nCount--);
  367. }
复制代码

交通灯 max.7z

203.96 KB, 下载次数: 89, 下载积分: 黑币 -5

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:1 发表于 2019-1-8 21:32 | 只看该作者
补全原理图或者详细说明一下电路连接即可获得100+黑币
回复

使用道具 举报

板凳
ID:464849 发表于 2019-1-9 10:19 | 只看该作者
原理图在此

32.png (45.51 KB, 下载次数: 132)

32.png

LED灯.png (18.04 KB, 下载次数: 119)

LED灯.png

晶振.png (15.32 KB, 下载次数: 157)

晶振.png

数码管电路.png (38.22 KB, 下载次数: 119)

数码管电路.png

总体原理图.png (61.86 KB, 下载次数: 124)

总体原理图.png

最小系统.png (14.31 KB, 下载次数: 111)

最小系统.png

评分

参与人数 1黑币 +20 收起 理由
admin + 20 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

地板
ID:564646 发表于 2019-6-23 16:04 | 只看该作者
楼主,PCB的图有吗,新手小白想联系一下布线
回复

使用道具 举报

5#
ID:386703 发表于 2019-6-25 14:24 | 只看该作者
楼主,请问有Proteus的仿真图吗
回复

使用道具 举报

6#
ID:666560 发表于 2019-12-17 18:32 | 只看该作者
请问楼主有delay.h文件吗?方便发一下吗?
回复

使用道具 举报

7#
ID:880524 发表于 2021-6-4 19:55 | 只看该作者
大佬有没有仿真源文件
回复

使用道具 举报

8#
ID:1007094 发表于 2022-4-11 17:37 | 只看该作者

有没有图纸文件
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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