#include "stm32f10x.h"
#include
#include "lcd.h"
#include "led.h"
u32 TimingDelay = 0;
u16 LEDRead = 0xffff;
u32 oneS , oneS2, twoS, fourS, fourS2, sixS;
u8 str[20];
u16 liushui = 0x1000;
u16 ledshan[4] = {0xefff, 0xdfff, 0xbfff, 0x7fff};
u8 k = 1;
typedef struct
{
u8 start;
u8 up;
u8 down;
u8 menok; // 关门ok
u8 yidongok; // 移动电梯ok
u8 menKok; //开门ok
u8 shan; //流水灯标志
u8 yunxing; //运行标志
u8 anjian; //按键一秒标志
u8 shanlcd; //平台号lcd闪烁
}FLAG;
FLAG flag = {0,1,0,0,0,0,1,0,0,0};
u8 Level = 1;
u8 level[6] = {0,0,0,0,0,0}; //level[1-4]分别表示某层是否有按下
u8 j;//保存当前需要处理的楼层
void Delay_Ms(u32 nTime);
void Lcd_init(void);
void Gpio_init(void);
void Led_OFF(uint16_t led);
void Led_ON(uint16_t led);
void Led_turn(uint16_t led);
u8 Key_scan(void);
void Key_check(void);
void show(void);
void Rtc_init(void);
void rtcShow(void);
void pwm_init(uint16_t period, uint16_t CCR1_Val, uint16_t CCR2_Val);
void yunxing(void);
//Main Body
int main(void)
{
SysTick_Config(SystemCoreClock/1000);
Lcd_init();
Gpio_init();
Rtc_init();
pwm_init(999,699,599);
while(1)
{
Key_check();
yunxing();
show();
rtcShow();
}
}
void yunxing(void)
{
u8 i;
if(flag.anjian == 1)
{
if(flag.up == 1)
{
for(i = Level ; i <= 5;i++)
{
if(level[i] == 2)
break; //如果正在运行,则直接退出
if(level[i] == 1 ) //如果有待运行的楼层,则标记
{
flag.start = 1;
j = i;
break;
}
if(i == 5)
{
j = 5;
flag.up = 0;
flag.down = 1;
flag.start = 0; //如果没搜到
}
}
if(flag.start == 1) //关门
{
flag.start = 0;
level[j] = 2; //标记为正在处理
pwm_init(499,0,249);
fourS = 4000;
}
if(flag.menok == 1) //上升
{
flag.yunxing = 1;
flag.menok = 0;
pwm_init(999,799,0);
sixS = 6000*(j - Level);
}
if(flag.yidongok == 1) //开门
{
flag.yunxing = 0;
liushui = 0x0000;
flag.yidongok = 0;
pwm_init(499,299,0);
fourS2 = 4000;
Level = j; //到了
flag.shanlcd = 1;
oneS = 300; //lcd闪烁
}
if(flag.menKok == 1)
{
flag.menKok = 0;
level[j] = 0;
}
// sprintf((char *)str," %d %d %d %d ",flag.start, flag.menok, flag.yidongok, flag.menKok);
// LCD_DisplayStringLine(Line5,str);
// sprintf((char *)str," %d %d %d %d %d ",level[1] , level[2] , level[3] , level[4] ,j);
// LCD_DisplayStringLine(Line6,str);
if(j == 5)
{
flag.up = 0;
flag.down = 1;
flag.start = 0; //如果没搜到
flag.anjian = 0; //按键按下一秒之后才能够运行。 现在结束了
}
// sprintf((char *)str," %d %d ",flag.up , flag.down);
// LCD_DisplayStringLine(Line1,str);
}
if(flag.down == 1)
{
for(i = Level ; i >= 0;i--)
{
if(level[i] == 2)
break; //如果正在运行,则直接退出
if(level[i] == 1 ) //如果有待运行的楼层,则标记
{
flag.start = 1;
j = i;
break;
}
if(i == 0)
{
j = i;
flag.up = 1;
flag.down = 0;
flag.start = 0; //如果没搜到
}
}
if(flag.start == 1) //关门
{
flag.start = 0;
level[j] = 2; //标记为正在处理
pwm_init(499,0,249);
fourS = 4000;
}
if(flag.menok == 1) //下降
{
flag.yunxing = 1;
liushui = 0x8000;
flag.menok = 0;
pwm_init(999,0,599);
sixS = 6000*(Level - j);
}
if(flag.yidongok == 1) //开门
{
flag.yunxing = 0;
liushui = 0x0000;
flag.yidongok = 0;
pwm_init(499,299,0);
fourS2 = 4000;
Level = j; //到了
flag.shanlcd = 1;
oneS = 300; //lcd闪烁
}
if(flag.menKok == 1)
{
flag.menKok = 0;
level[j] = 0;
}
// sprintf((char *)str," %d %d %d %d ",flag.start, flag.menok, flag.yidongok, flag.menKok);
// LCD_DisplayStringLine(Line5,str);
// sprintf((char *)str," %d %d %d %d %d ",level[1] , level[2] , level[3] , level[4] ,j);
// LCD_DisplayStringLine(Line6,str);
if(j == 0)
{
flag.up = 1;
flag.down = 0;
flag.start = 0; //如果没搜到
flag.anjian = 0; //按键按下一秒之后才能够运行。 现在结束了
}
// sprintf((char *)str," %d %d ",flag.up , flag.down);
// LCD_DisplayStringLine(Line1,str);
}
}
}
//PA6 TIM3_CH1
//PA7 TIM3_CH2
//上行 pwm_init(999,799,0)
//下行 pwm_init(999,0,599)
//开门 pwm_init(499,299,0)
//关门 pwm_init(499,0,249)
//停止 pwm_init (999,0,0)
void pwm_init(uint16_t period, uint16_t CCR1_Val, uint16_t CCR2_Val)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = period;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_Cmd(TIM3, ENABLE);
}
void show(void)
{
LCD_DisplayStringLine(Line2,(unsigned char *)" Local Loacation ");
// LCD_DisplayStringLine(Line3,(unsigned char *)" ");
sprintf((char *)str," %d ",Level);
if(flag.shanlcd <=3 && flag.shan == 1)
{
flag.shanlcd ++;
flag.shan = 0;
oneS = 300;
sprintf((char *)str," ");
}
LCD_DisplayStringLine(Line4,str);
// LCD_DisplayStringLine(Line4,(unsigned char *)" 1 ");
// LCD_DisplayStringLine(Line5,(unsigned char *)" ");
// LCD_DisplayStringLine(Line6,(unsigned char *)" ");
// LCD_DisplayStringLine(Line8,(unsigned char *)" ");
// LCD_DisplayStringLine(Line9,(unsigned char *)" ");
// if(flag.shan == 1)
// {
// flag.shan = 0;
if(flag.up == 1 && flag.yunxing == 1)
{
if(k == 1)
{
Led_OFF(LED7 | LED5 | LED6);
Led_ON(LED4);
}
if(k == 2)
{
Led_OFF(LED7 | LED4 | LED6);
Led_ON(LED5);
}
if(k == 3)
{
Led_OFF(LED7 | LED5 | LED8);
Led_ON(LED6);
}
if(k == 4)
{
Led_OFF(LED7 | LED5 | LED6);
Led_ON(LED8);
k = 0;
}
k++;
}
if(flag.down == 1 && flag.yunxing == 1)
{
if(k == 1)
{
Led_OFF(LED7 | LED5 | LED6);
Led_ON(LED4);
k = 4;
}
if(k == 2)
{
Led_OFF(LED7 | LED4 | LED6);
Led_ON(LED5);
}
if(k == 3)
{
Led_OFF(LED7 | LED5 | LED8);
Led_ON(LED6);
}
if(k == 4)
{
Led_OFF(LED7 | LED5 | LED6);
Led_ON(LED8);
}
k--;
}
if(flag.yunxing == 0)
Led_OFF(LED7 | LED5 | LED6 | LED8);
// }
if(Level == 1)
{
Led_OFF(LED2 | LED3 | LED4);
Led_ON(LED1);
}
if(Level == 2)
{
Led_OFF(LED1 | LED3 | LED4);
Led_ON(LED2);
}
if(Level == 3)
{
Led_OFF(LED1 | LED2 | LED4);
Led_ON(LED3);
}
if(Level == 4)
{
Led_OFF(LED2 | LED3 | LED1);
Led_ON(LED4);
}
}
void Rtc_init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
BKP_DeInit();
RCC_LSICmd(ENABLE);
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
RTC_WaitForLastTask();
RTC_ITConfig(RTC_IT_SEC, ENABLE);
RTC_WaitForLastTask();
RTC_SetPrescaler(39999); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
RTC_WaitForLastTask();
/* Change the current time */
RTC_SetCounter(12*3600 + 50*60 + 55);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
void rtcShow(void)
{
uint32_t THH = 0, TMM = 0, TSS = 0;
uint32_t TimeVar = RTC_GetCounter();
/* Reset RTC Counter when Time is 23:59:59 */
if (TimeVar == 0x00015180)
{
RTC_SetCounter(0x0);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
TimeVar = RTC_GetCounter();
}
/* Compute hours */
THH = TimeVar / 3600;
/* Compute minutes */
TMM = (TimeVar % 3600) / 60;
/* Compute seconds */
TSS = (TimeVar % 3600) % 60;
sprintf((char *)str," %0.2d:%0.2d:%0.2d ", THH, TMM, TSS);
LCD_DisplayStringLine(Line7,str);
}
void Key_check(void)
{
u8 Key_Value = Key_scan();
if(Key_Value == 1)
{
if(Level != 1)
level[1] = 1;
}
if(Key_Value == 2)
{
if(Level != 2)
level[2] = 1;
}
if(Key_Value == 3)
{
if(Level != 3)
level[3] = 1;
}
if(Key_Value == 4)
{
if(Level != 4)
level[4] = 1;
}
// sprintf((char *)str," %d ",Key_Value);
// LCD_DisplayStringLine(Line1,str);
}
u8 Key_scan(void)
{
if(RB1 == 0)
{
Delay_Ms(5);
if(RB1 == 0)
{
// LCD_DisplayStringLine(Line0,(unsigned char *)" 1111 ");
oneS2 = 1000;
while(RB1 == 0);
return 1;
}
}
if(RB2 == 0)
{
Delay_Ms(5);
if(RB2 == 0)
{
// LCD_DisplayStringLine(Line0,(unsigned char *)" 2222 ");
oneS2 = 1000;
while(RB2 == 0);
return 2;
}
}
if(RB3 == 0)
{
Delay_Ms(5);
if(RB3 == 0)
{
// LCD_DisplayStringLine(Line0,(unsigned char *)" 333 ");
oneS2 = 1000;
while(RB3 == 0);
return 3;
}
}
if(RB4 == 0)
{
Delay_Ms(5);
if(RB4 == 0)
{
// LCD_DisplayStringLine(Line0,(unsigned char *)" 444 ");
oneS2 = 1000;
while(RB4 == 0);
return 4;
}
}
return 0;
}
void Gpio_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |RCC_APB2Periph_GPIOD |
RCC_APB2Periph_AFIO, ENABLE);
/* ******************************LED_Init*********************** */
GPIO_InitStructure.GPIO_Pin = LEDALL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ;
GPIO_Init(GPIOD, &GPIO_InitStructure);
Led_OFF(LEDALL);
}
void Led_OFF(uint16_t led)
{
GPIOC->ODR = LEDRead;
GPIO_SetBits(GPIOC, led);
GPIO_SetBits(GPIOD, GPIO_Pin_2);
GPIO_ResetBits(GPIOD, GPIO_Pin_2);
LEDRead = GPIOC->ODR;
}
void Led_ON(uint16_t led)
{
GPIOC->ODR = LEDRead;
GPIO_ResetBits(GPIOC, led);
GPIO_SetBits(GPIOD, GPIO_Pin_2);
GPIO_ResetBits(GPIOD, GPIO_Pin_2);
LEDRead = GPIOC->ODR;
}
void Led_turn(uint16_t led)
{
GPIOC->ODR = LEDRead;
LEDRead ^= led;
GPIOC->ODR = LEDRead;
GPIO_SetBits(GPIOD, GPIO_Pin_2);
GPIO_ResetBits(GPIOD, GPIO_Pin_2);
LEDRead = GPIOC->ODR;
}
void Lcd_init(void)
{
STM3210B_LCD_Init();
LCD_Clear(Blue);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
// LCD_DisplayStringLine(Line0,(unsigned char *)" ok ");
// LCD_DisplayStringLine(Line1,(unsigned char *)" ");
LCD_DisplayStringLine(Line2,(unsigned char *)" ");
LCD_DisplayStringLine(Line3,(unsigned char *)" ");
LCD_DisplayStringLine(Line4,(unsigned char *)" ");
LCD_DisplayStringLine(Line5,(unsigned char *)" ");
LCD_DisplayStringLine(Line6,(unsigned char *)" ");
LCD_DisplayStringLine(Line7,(unsigned char *)" ");
LCD_DisplayStringLine(Line8,(unsigned char *)" ");
LCD_DisplayStringLine(Line9,(unsigned char *)" ");
}
//
void Delay_Ms(u32 nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
void SysTick_Handler(void)
{
TimingDelay--;
if(--oneS == 0)
{
flag.shan = 1;
}
if(--oneS2 == 0)
{
flag.anjian = 1;
}
if(--fourS == 0) //关门
{
flag.menok = 1;
}
if(--fourS2 == 0) //开门
{
flag.menKok = 1;
}
if(--sixS == 0)
{
flag.yidongok = 1;
}
}