|
基于stm32的光立方灰度显示程序
- #include "stm32f10x.h"
- #include "pwm.h"
- #include "spi.h"
-
- #define TOTALPeriod 1000
- #define LIGTHNESS_0 TOTALPeriod-(TOTALPeriod/100)
- #define LIGTHNESS_1 TOTALPeriod-(TOTALPeriod/100*5)
- #define LIGTHNESS_2 TOTALPeriod-(TOTALPeriod/100*20)
- #define LIGTHNESS_3 TOTALPeriod-(TOTALPeriod/100*40)
- #define LIGTHNESS_4 TOTALPeriod-(TOTALPeriod/100*90)
- u8 layer_cnt,light_cnt;
- char frame_data[5][64]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};
- unsigned int all_cnt;
- void TIM1_PWM_Init()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //使能GPIO外设时钟使能
-
- //设置该引脚为复用输出功能,输出TIM1 CH1的PWM脉冲波形
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM_CH1
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
- TIM_TimeBaseStructure.TIM_Period = TOTALPeriod; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 80K
- TIM_TimeBaseStructure.TIM_Prescaler =11; //设置用来作为TIMx时钟频率除数的预分频值 12M
- TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
- TIM_OCStructInit(&TIM_OCInitStructure);
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式1
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- TIM_OCInitStructure.TIM_Pulse = LIGTHNESS_0; //设置待装入捕获比较寄存器的脉冲值
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
- TIM_OC1Init(TIM1, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
- TIM_SelectOnePulseMode(TIM1, TIM_OPMode_Single);
- TIM_CtrlPWMOutputs(TIM1,ENABLE); //MOE 主输出使能
- TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH1预装载使能
-
- TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIMx在ARR上的预装载寄存器
-
-
-
- }
- void TIM2_Init()
- {
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//
- TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
- TIM_TimeBaseStructure.TIM_Period = 250; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
- TIM_TimeBaseStructure.TIM_Prescaler =71; //设置用来作为TIMx时钟频率除数的预分频值 1M
- TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
-
- TIM_ARRPreloadConfig(TIM2, ENABLE); //使能TIMx在ARR上的预装载寄存器
- TIM_ITConfig(TIM2, TIM_IT_Update,ENABLE);
- /* Enable the USART1 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- TIM_Cmd(TIM2, ENABLE); //使能TIM2
- }
- void delay(int cnt)
- {
- while(cnt!=0) cnt--;
- }
-
- void display_layer( char ly,char* pdata)
- { u8 i;
-
-
-
- SPI2_ReadWriteByte(0x01<<ly);
- for( i=0;i<8;i++)
- {
- SPI2_ReadWriteByte(*(pdata+i));
- }
- GPIO_ResetBits(GPIOB,GPIO_Pin_11);
- delay(100);
- GPIO_SetBits(GPIOB,GPIO_Pin_11);
-
- }
- void set_lightness(u8 lg)
- {
- switch(lg)
- {
- case 0:
- TIM_SetCompare1(TIM1,LIGTHNESS_0);
- break;
- case 1:
- TIM_SetCompare1(TIM1,LIGTHNESS_1);
- break;
- case 2:
- TIM_SetCompare1(TIM1,LIGTHNESS_2);
- break;
- case 3:
- TIM_SetCompare1(TIM1,LIGTHNESS_3);
- break;
- case 4:
- TIM_SetCompare1(TIM1,LIGTHNESS_4);
- break;
- default:
- break;
- }
-
- }
- void tim2_handler()
- {
- if(layer_cnt>=8)
- {
- layer_cnt=0;
- light_cnt++;
- if(light_cnt>=5)
- light_cnt=0;
- }
-
- display_layer(layer_cnt,frame_data[light_cnt]+layer_cnt*8);
- set_lightness(light_cnt);
- TIM_Cmd(TIM1, ENABLE);
- layer_cnt++;
- }
- void TIM2_IRQHandler(void)
- {
- if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
- {
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
- tim2_handler();
- all_cnt++;
- }
- }
复制代码
|
评分
-
查看全部评分
|