1. 程式人生 > >第七屆藍橋杯嵌入式程式碼

第七屆藍橋杯嵌入式程式碼

#include "stm32f10x.h"
#include <stdio.h>
#include "lcd.h"
#include "led.h"
#include "i2c.h"

typedef struct
{
	u8 	show1;
	u8  cai;
	u8  usart;
	u8  c;
	u8  s;
	u8  change;
	u8  qushi;
	u8  set;
	u8  wuci;
	u8  wuci2;
}Flag;

Flag  flag = {1,1,0,0,0,0,3,1,10,10};

u32 TimingDelay = 0;
float ADC_Value;
float pre_ADC_Value;
u32 ADC_Height;
u8 ADC_Level;
u16 Timeing1s;
u16	Timeing200ms;
u8 str[20];
u8 low,mid,high;
__IO uint8_t RxCounter1; 
uint8_t RxBuffer1[15];
uint16_t ReadLED = 0xff;	
		  
void Delay_Ms(u32 nTime);
void Lcd_init(void);
void Gpio_init(void);
u8 Key_scan(void);
void key_check(void);
void show(void);
void Adc_init(void);
void Get_ADC(void);
void Usart_init(void);
void NVIC_Configuration(void);
void sendData(void);
void Led_turn(uint16_t LED);
void Led_OFF(uint16_t LED);


//Main Body
int main(void)
{
	SysTick_Config(SystemCoreClock/1000);
	Lcd_init();
	Gpio_init();
	Adc_init();	
	i2c_init();	
	Usart_init();

	if(i2cRead(0x20) != 20)
	{
		i2cWrite(0x00,30);
		i2cWrite(0x01,50);
		i2cWrite(0x02,70);
		i2cWrite(0x20,20);
	}
 
	low = i2cRead(0x00);
	mid = i2cRead(0x01);
	high = i2cRead(0x02);

	while(1)
	{
		key_check();
		Get_ADC();
		show();
		sendData();	
	}

}


void show(void)
{
	if(flag.show1 == 1)
	{
			LCD_DisplayStringLine(Line0,(unsigned char *)"                    ");
			LCD_DisplayStringLine(Line1,(unsigned char *)"   Liquid Level     ");	
			LCD_DisplayStringLine(Line2,(unsigned char *)"                    ");
			ADC_Height = (u32)(ADC_Value * 30.303);
			sprintf((char *)str," Height: %dcm         ",ADC_Height);
			LCD_DisplayStringLine(Line3,str);
			LCD_DisplayStringLine(Line4,(unsigned char *)"                    ");	
			sprintf((char *)str," ADC: %.2fV           ",ADC_Value);				
			LCD_DisplayStringLine(Line5,str);	
			LCD_DisplayStringLine(Line6,(unsigned char *)"                    ");
			
			pre_ADC_Value =  ADC_Level;

			if(ADC_Height > high)
			ADC_Level = 3;
			if(ADC_Height <= high && ADC_Height > mid)
			ADC_Level = 2;
			if(ADC_Height <= mid && ADC_Height > low)
			ADC_Level = 1;
			if(ADC_Height <= low)
			ADC_Level = 0;

			if(ADC_Level != pre_ADC_Value)
			{	
				flag.change = 1;
				flag.wuci = 10;
			}
			
			if(ADC_Level < pre_ADC_Value)
				flag.qushi = 2;
			if(ADC_Level > pre_ADC_Value)
				flag.qushi = 1;

			sprintf((char *)str," Level: %d           ",ADC_Level);				
			LCD_DisplayStringLine(Line7,str);	
			LCD_DisplayStringLine(Line8,(unsigned char *)"                    ");		
			LCD_DisplayStringLine(Line9,(unsigned char *)"                    ");		
	}
	else 
	{	
			LCD_DisplayStringLine(Line0,(unsigned char *)"                    ");
			LCD_DisplayStringLine(Line1,(unsigned char *)"   Parameter Setup  ");	
			LCD_DisplayStringLine(Line2,(unsigned char *)"                    ");
		
			if(flag.set == 1)
				LCD_SetTextColor(Red);
			sprintf((char *)str," Threshold 1: %dcm        ",low);
			LCD_DisplayStringLine(Line3,str);
			if(flag.set == 1)	
				LCD_SetTextColor(White);
		
			LCD_DisplayStringLine(Line4,(unsigned char *)"                    ");
		
			if(flag.set == 2)
				LCD_SetTextColor(Red);	
			sprintf((char *)str," Threshold 2: %dcm        ",mid);
			LCD_DisplayStringLine(Line5,str);				
			if(flag.set == 2)	
				LCD_SetTextColor(White);

				
			LCD_DisplayStringLine(Line6,(unsigned char *)"                    ");

			if(flag.set == 3)
				LCD_SetTextColor(Red);
			sprintf((char *)str," Threshold 3: %dcm        ",high);	
			LCD_DisplayStringLine(Line7,str);	
			if(flag.set == 3)
				LCD_SetTextColor(White);

			LCD_DisplayStringLine(Line8,(unsigned char *)"                    ");		
			LCD_DisplayStringLine(Line9,(unsigned char *)"                    ");			
	}

}

void sendData(void)
{	
	if(flag.c == 1)
	{
		flag.c = 0;
	
		USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
	
		printf("C:H%d+L%d\r\n", ADC_Height, ADC_Level); 
		
		USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
	}

	if(flag.s == 1)
	{
		flag.s = 0;
	
		USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
	
		printf("S:TL%d+TM%d+TH%d\r\n", low, mid, high);  
		
		USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
	} 
	
	if(flag.change == 1)
	{	
		flag.change = 0;

		USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
	
		if(flag.qushi == 1)
			printf("A:H%d+L%d+U\r\n", ADC_Height, ADC_Level );
		else if(flag.qushi == 2) 
			printf("A:H%d+L%d+D\r\n", ADC_Height, ADC_Level );
		
		flag.qushi = 3;  
		
		USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
	}
}


int fputc(int ch, FILE *f)
{
	 USART_SendData(USART2, (uint8_t) ch);
	 while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
 		 return ch;	
}

void key_check(void)
{	
	u8 Kvalue;
	Kvalue = Key_scan();
	if(Kvalue == 1)
	{
		Kvalue  = 0;
		flag.show1 = ~flag.show1;
		flag.set = 1;
	}

	if(Kvalue == 2)
	{	
		if(flag.show1 != 0)
		{
			flag.set++;
			if(flag.set == 4)
			flag.set = 1;
		}	
		Kvalue  = 0;
	}

	if(Kvalue == 3)
	{
		if(flag.show1 != 0)
		{
			if(flag.set == 1)
			{
				low += 5;
				if(low == mid)
				low -= 5;
			}

			if(flag.set == 2)
			{
				mid += 5;
				if(mid == high)
				mid -= 5;
			}

			if(flag.set == 3)
			{
				high += 5;
				if(high == 105)
				low -= 5;
			}
		}
		Kvalue  = 0;

	   	i2cWrite(0x00,low);
		i2cWrite(0x01,mid);
		i2cWrite(0x02,high);
	}

	if(Kvalue == 4)
	{
		if(flag.show1 != 0)
		{
			if(flag.set == 1)
			{
				low -= 5;
				if(low == 0)
				low += 5;
			}

			if(flag.set == 2)
			{
				mid -= 5;
				if(mid == low)
				mid += 5;
			}

			if(flag.set == 3)
			{
				high -= 5;
				if(high == mid)
				high += 5;
			}
		}
		Kvalue  = 0;

		i2cWrite(0x00,low);
		i2cWrite(0x01,mid);
		i2cWrite(0x02,high);
	}

}

u8 Key_scan(void)
{	
	if(RB1 == 0)
	{
		Delay_Ms(5);
		if(RB1 == 0)
		{
		   	 while(!RB1);
		   	 return 1;	
		}	
	}

	if(RB2 == 0)
	{
		Delay_Ms(5);
		if(RB2 == 0)
		{
			  while(!RB2);
			  return 2;	
		}	
		
	}

	if(RB3 == 0)
	{
		Delay_Ms(5);
		if(RB3 == 0)
		{
		     while(!RB3);
			 return 3;	
		}	
		
	}

	if(RB4 == 0)
	{
		Delay_Ms(5);
		if(RB4 == 0)
		{
			while(!RB4);
	   		return 4;	
		}	
			
	}
	return 0;
}

void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Configure the NVIC Preemption Priority Bits */  
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  /* Enable the USARTz Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);


  /******************TIM2定時器中斷********************/
//  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
//  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
//  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//
//  NVIC_Init(&NVIC_InitStructure);
}

void Usart_init(void)
{	
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;


	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); 

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USARTy Rx as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  

  USART_InitStructure.USART_BaudRate = 9600;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  
  USART_Init(USART2, &USART_InitStructure);
  

  /* Enable USARTz Receive and Transmit interrupts */
  USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  //USART_ITConfig(USART2, USART_IT_TXE, ENABLE);   //不知道為什麼,如果開了這個,則會導致錯誤

  USART_Cmd(USART2, ENABLE);
  NVIC_Configuration();
} 

void Get_ADC(void)
{
	if(flag.cai == 1)
	{
		ADC_SoftwareStartConvCmd(ADC1, ENABLE);
		ADC_Value = ADC_GetConversionValue(ADC1) * 3.3 / 0xfff;
		flag.cai = 0;
	}
}
void Adc_init(void)
{	
	ADC_InitTypeDef  ADC_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
	
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
	GPIO_Init(GPIOB, &GPIO_InitStructure);


	ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
	ADC_InitStructure.ADC_ScanConvMode = DISABLE;
	ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
	ADC_InitStructure.ADC_NbrOfChannel = 1;
	ADC_Init(ADC1, &ADC_InitStructure);
	
	ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_13Cycles5);
	ADC_Cmd(ADC1, ENABLE);
	ADC_ResetCalibration(ADC1);
	while(ADC_GetResetCalibrationStatus(ADC1));
	ADC_StartCalibration(ADC1);
	while(ADC_GetCalibrationStatus(ADC1));	
	ADC_SoftwareStartConvCmd(ADC1, ENABLE);  	
}



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);						    

/**********************************KEY_INIT**********************************/
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

}

void Lcd_init(void)
{	
	STM3210B_LCD_Init();
	LCD_Clear(Blue);
	LCD_SetBackColor(Blue);
	LCD_SetTextColor(White);
	
	LCD_DisplayStringLine(Line0,(unsigned char *)"  		          ");	
	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 tim_init(void)
//{
//	  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
//   	
//	  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
//
//	  TIM_TimeBaseStructure.TIM_Period = 1999;
//	  TIM_TimeBaseStructure.TIM_Prescaler = 7199;
//	  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
//	  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//	
//	  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
//
//	  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
//
//	  TIM_Cmd(TIM2, ENABLE);
//}

void Led_turn(uint16_t LED)
{
		GPIOC->ODR = ReadLED;	
		ReadLED ^= LED;
		GPIOC->ODR = ReadLED;
		GPIO_SetBits(GPIOD, GPIO_Pin_2);		
		GPIO_ResetBits(GPIOD, GPIO_Pin_2);
	    ReadLED = GPIOC->ODR;
}

void Led_OFF(uint16_t LED)
{
	 	GPIOC->ODR = ReadLED;	
		GPIO_SetBits(GPIOC, LED);
 		GPIO_SetBits(GPIOD, GPIO_Pin_2);				 		
		GPIO_ResetBits(GPIOD, GPIO_Pin_2);
   	  	ReadLED = GPIOC->ODR;
}
void Delay_Ms(u32 nTime)
{
	TimingDelay = nTime;
	while(TimingDelay != 0);	
}

void SysTick_Handler(void)
{
		Timeing1s++;
		if(Timeing1s == 999)
		{
			Timeing1s = 0;		
			flag.cai = 1;
			Led_turn( LED1);
		}

		Timeing200ms++;
		if(Timeing200ms == 199)
		{
			Timeing200ms = 0;		
			if(flag.wuci != 0)
			{
				flag.wuci--;
				Led_turn( LED2);

				if(flag.wuci == 0)
					Led_OFF(LED2);
				
			}
		
			if(flag.wuci2 != 0)
			{
				flag.wuci2--;
				Led_turn( LED3);

				if(flag.wuci2 == 0)
					Led_OFF(LED3);
					
			}
		}
		TimingDelay--;
}

void USART2_IRQHandler(void) 
{
   u8 USART2_RECEIVED;

  if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) 
  {	
	  USART2_RECEIVED=USART_ReceiveData(USART2);
	  RxBuffer1[RxCounter1++] = USART2_RECEIVED;
  }
  if(USART2_RECEIVED == '\n')
  {
 	 USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);

	 if(RxBuffer1[0] == 'C')
	 {
	 	flag.c  = 1;
		flag.wuci2 = 10;
	 }
	 if(RxBuffer1[0] == 'S')
	 {
	 	flag.s  = 1; 
		flag.wuci2 = 10;	 
	 }
	 RxCounter1 = 0;								 //關閉中斷之後,效果會很好,但是一旦時間長了之後,還是會造成錯誤,可能是硬體長期的執行導致的錯誤
	
	 USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  }
}



#ifndef __Led_H
#define __Led_H


#define LED1 GPIO_Pin_8              
#define LED2 GPIO_Pin_9               
#define LED3 GPIO_Pin_10              
#define LED4 GPIO_Pin_11             
#define LED5 GPIO_Pin_12               
#define LED6 GPIO_Pin_13                
#define LED7 GPIO_Pin_14                
#define LED8 GPIO_Pin_15              
#define LEDALL GPIO_Pin_All

#define RB1 GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)
#define RB2 GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8)
#define RB3 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)
#define RB4 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_2)
	  	
#endif
void i2cWrite(unsigned char add,unsigned char data)
{
	I2CStart();
	I2CSendByte(0xa0);
	I2CSendAck();
	I2CSendByte(add);
	I2CSendAck();
	I2CSendByte(data);
	I2CSendAck();
	I2CStop();
	Delay_Ms(10);		
}

unsigned char i2cRead(unsigned char add)
{
	unsigned char temp;
	I2CStart();
	I2CSendByte(0xa0);
	I2CSendAck();
	I2CSendByte(add);
	I2CSendAck();

	I2CStart();
	I2CSendByte(0xa1);
	I2CSendAck();
	temp = I2CReceiveByte();
	I2CStop();
	Delay_Ms(10);
	return temp;		
}