簡易電子秤程序,在此分享,歡迎下載
單片機源程序如下:
- #include "stm32f4xx.h"
- #include "stm32f4xx_conf.h"
- #include "stm32f4xx_it.h"
- #include <stdio.h> //C語言頭文件
- #include "LCD.h"
- #include "math.h" //C語言的頭文件
- #include "WAVEDAT.h"
- #define KEY_RAM (*((volatile unsigned short *) 0x60070000)) //外部地址是從0x60000000開始的
- GPIO_InitTypeDef GPIO_InitStructure;
- DAC_InitTypeDef DAC_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- ADC_InitTypeDef ADC_InitStructure;
- ADC_CommonInitTypeDef ADC_CommonInitStructure;
- u16 volatile i,j;
- u8 posbit=0x00;
- u8 posbit1=0x00;
- u8 posbit2=0x00;
- u16 DACDAT;
- u8 keysign=0;
- u8 keycode;
- u8 duty=0x02;
- //u16 count;
- u8 sindat[256];
- char temp[3];
- u8 k;
- u16 timer,tdata;
- u8 RANGE;
- //u16 adc_value; //存放A/D轉換值
- //u8 ADCSIGN; //A/D轉換結束標志
- //*********************************
- u32 price=0;//單價
- u32 count=0;//計價
- u32 sum=0;//累計
- u32 weight=0;//質量
- u32 weight1=0;
- u32 tare=0;//皮重
- u8 flag_int=1;//整數小數輸入標志,1=整數
- u8 flag_price=0;//單價輸入標志,1=正在輸入單價
- u8 num_num=0;
- u32 number=0;
- u8 number1=0;
- u8 number2=0;
- u32 value1[5];
- //*********************************
- void FSMC_init(void);
- void GPIO_Configuration(void);
- void EXTI_init(void);
- void TIM1_init(void);
- void TIM2_init(void);
- void TIM1INT_init(void);
- void TIM2INT_init(void);
- void DAC_init(void);
- //void ADC_init(void); //ADC初始化
- void PE0Tog(void);
- void PE3Tog(void);
- void PE2Tog(void);
- void SysClk_Init(u32 clock);
- void delay_ms(volatile u16 time);
- //*********************************
- // IIC 初始化
- //*********************************
- void I2C_Configuration(void)
- {
- I2C_InitTypeDef I2C_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- I2C_DeInit(I2C1);
- I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
- I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
- I2C_InitStructure.I2C_OwnAddress1 = 0x91; //地址
- I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; //ack enable
- I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
- I2C_InitStructure.I2C_ClockSpeed = 100000; //100k
- I2C_Cmd(I2C1, ENABLE);
- I2C_Init(I2C1, &I2C_InitStructure);
- //I2C_AcknowledgeConfig(I2C1, ENABLE);
- }
- //*********************************
- // IIC Read
- //*********************************
- char value[2];
- void I2C_Read(void)
- {
- /*
- while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY))
- {;}
- I2C_AcknowledgeConfig(I2C1, ENABLE);
- I2C_GenerateSTART(I2C1, ENABLE);
- while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
- {;}
- I2C_Send7bitAddress(I2C1, 0x91, I2C_Direction_Transmitter);
- while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
- {;}
- I2C_SendData(I2C1, addr);
- while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
- {;}
- */
- I2C_GenerateSTART(I2C1, ENABLE);
- while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
- {;}
- I2C_Send7bitAddress(I2C1, 0x91, I2C_Direction_Receiver);
- while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
- {;}
- while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) // EV7
- {;}
- value[0] = I2C_ReceiveData(I2C1);
- I2C_AcknowledgeConfig(I2C1, DISABLE);
- I2C_GenerateSTOP(I2C1, ENABLE);
- while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) // EV7
- {;}
- value[1] = I2C_ReceiveData(I2C1);
- //cal_data = value[0]<<8 | value[1];
- //printf("buf[0] = %x,buf[1] = %x,%d,%d\r\n",buf[0],buf[1],(buf[0]<<8|buf[2]),cal_data);
- // Decrement the read bytes counter
- I2C_AcknowledgeConfig(I2C1, ENABLE);
- }
- //*********************************
- // 四位整數兩位小數顯示
- //*********************************
- void printnum(u16 x,u16 y,u32 num,u16 color)
- {
- u32 temp;
- temp=num/100;
- LCD_ShowNumBig_L(x+48,x-16,y,temp,color);
-
- LCD_ShowCharBig(x+48,y,'.',color);
-
- temp=num/10%10;
- LCD_ShowCharBig(x+64,y,temp+'0',color);
- temp=num%10;
- LCD_ShowCharBig(x+80,y,temp+'0',color);
- }
- //*********************************
- // 質量刷新
- //*********************************
- u32 weight2=0;
- void printweight(void)
- {
- printnum(120,190,weight2,BLACK);
- printnum(120,190,weight-tare,WHITE);
- weight2=weight-tare;
- }
- //*********************************
- // 累計刷新
- //*********************************
- u32 sum2=0;
- void printsum(void)
- {
- printnum(350,80,sum2,BLACK);
- printnum(350,80,sum,WHITE);
- sum2=sum;
- }
- //*********************************
- // 單價刷新
- //*********************************
- u32 price2=0;
- void printprice(void)
- {
- printnum(120,80,price2,BLACK);
- printnum(120,80,price,WHITE);
- price2=price;
- }
- //*********************************
- // 計價刷新
- //*********************************
- u32 count2=0;
- void printcount(void)
- {
- printnum(350,190,count2,BLACK);
- printnum(350,190,count,WHITE);
- count2=count;
- }
- //*********************************
- // 按鍵處理
- //*********************************
- u16 temp_price_high=0;
- u16 temp_price_low=0;
- void keycontrol(void)
- {
- if(keysign==1)
- {
- keysign=0;
- if(keycode==0x07)//單價
- {
- price=0;
- printprice();
- temp_price_high=0;
- temp_price_low=0;
- flag_price=1;
- flag_int=1;
- num_num=0;
- }
- else if(keycode==0x03&&flag_price==1)//確定
- {
- flag_price=0;
- price=temp_price_high*100+temp_price_low;
- printcount();
- }
- else if(keycode==0x0f)//去皮
- {
- tare=weight;
- }
- else if(keycode==0x0e)//復位
- {
- price=0;
- temp_price_high=0;
- temp_price_low=0;
- count=0;
- sum=0;
- flag_price=0;
- flag_int=1;
- tare=0;
- num_num=0;
- printweight();
- printsum();
- printprice();
- printcount();
- }
- else if(keycode==0x0d&&flag_price==1)//小數點
- {
- flag_int=0;
- num_num=0;
- }
- else if(keycode==0x0b)//累加
- {
- sum=sum+count;
- printsum();
- }
- else if(flag_price==1)//數字按鍵
- {
- u16 temp=0;
- switch(keycode)
- {
- case 0x0c:
- {
- temp=0;
- break;
- }
- case 0x08:
- {
- temp=1;
- break;
- }
- case 0x09:
- {
- temp=2;
- break;
- }
- case 0x0a:
- {
- temp=3;
- break;
- }
- case 0x04:
- {
- temp=4;
- break;
- }
- case 0x05:
- {
- temp=5;
- break;
- }
- case 0x06:
- {
- temp=6;
- break;
- }
- case 0x00:
- {
- temp=7;
- break;
- }
- case 0x01:
- {
- temp=8;
- break;
- }
- case 0x02:
- {
- temp=9;
- break;
- }
- }
- if(flag_int==1)//如果輸入整數
- {
- num_num++;
- if(num_num<=3)
- {
- temp_price_high=temp_price_high*10+temp;
- }
- }
- else//輸入單價小數
- {
- num_num++;
- if(num_num==1)
- {
- temp_price_low=temp*10;
- }
- if(num_num==2)
- {
- temp_price_low=temp_price_low+temp;
- }
- }
- price=temp_price_high*100+temp_price_low;
- printprice();
- }
-
- }
-
- }
- //*********************************
- // main
- //*********************************
- int main(void)
- {
- FSMC_init();
- LCD_Init(); //液晶初始化
- //外部中斷初始化
- EXTI_ClearITPendingBit(EXTI_Line1);
- GPIO_Configuration(); //I/O初始化
- TIM1_init();
- TIM2_init();
- DAC_init();
- //ADC_init();
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- EXTI_init();
- TIM1INT_init();
- TIM2INT_init();
- RCC_MCO1Config(RCC_MCO1Source_HSE,RCC_MCO1Div_4); //MCO選擇HSI,4分頻,因此PA8輸出3MHz方波
- SysClk_Init(1000);
-
- LCD_ShowChineseStringBig(160,270,8,5,YELLOW);
-
- LCD_ShowChineseStringBig(40,190,13,2,YELLOW);
- LCD_ShowChineseStringBig(270,190,19,2,YELLOW);
- LCD_ShowChineseStringBig(40,80,17,2,YELLOW);
- LCD_ShowChineseStringBig(270,80,15,2,YELLOW);
-
- TFTLED=0x01; //開啟背光
- keysign=0;
-
- I2C_Configuration();
-
- printweight();//顯示初始值
- printsum();
- printprice();
- printcount();
- while(1)
- {
- keycontrol();
- if(number1==1)
- {
- number1=0;
- /*
- I2C_Read();
- weight=value[0]*256+value[1];
- LCD_ShowNumBig(5,20,weight,WHITE);
- */
- if(number2<5)
- {
- I2C_Read();
- value1[number2]=value[0]*256+value[1];//高八位低八位合成
- number2++;
- }
- else
- {
- number2=0;
- weight=0;
- for(u8 i=0;i<5;i++)
- {
- weight+=value1[number2];
- }
- weight=weight/5;//五組數據取平均
- //LCD_ShowNumBig(5,20,weight,WHITE);
- weight=weight*15135-140000000;//質量換算
- weight=weight/1000;
- //LCD_ShowNumBig(205,20,weight,WHITE);
- weight=618;
- printweight();
- count=price*(weight-tare)/100;
- printcount();
- }
- }
- }
- }
- //----------------------------------------------------------------------------
- //定時器1中斷服務程序
- //----------------------------------------------------------------------------
- void TIM1_UP_TIM10_IRQHandler(void)
- {
- if ( TIM_GetITStatus(TIM1,TIM_IT_Update) != RESET ) //是否發生中斷
- {
-
- TIM_ClearITPendingBit(TIM1, TIM_IT_Update); //清除中斷待處理位
- PE2Tog();
-
- }
- }
- //----------------------------------------------------------------------------
- //定時器2中斷服務程序
- //----------------------------------------------------------------------------
- void TIM2_IRQHandler(void) //TIM2中斷
- {
- if(TIM_GetITStatus (TIM2,TIM_IT_Update)!=RESET)
- {
- TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
- PE3Tog();
- number++;
- if(number>100)
- {
- number=0;
- number1=1;
- }
- }
-
- }
- void EXTI0_IRQHandler(void) //鍵盤中斷
- {
- keycode = KEY_RAM;
- keycode &= 0x0f;
- keysign = 1;
-
- EXTI_ClearITPendingBit(EXTI_Line0);
-
- }
- /*
- void ADC_IRQHandler(void)
- {
- count++;
- adc_value = ADC_GetConversionValue(ADC1);
- ADC_ClearITPendingBit(ADC1,ADC_IT_EOC);
- if (count==500)
- {
- ADCSIGN=1;
- count=0;
- }
- }*/
- void SysTick_Handler(void)
- {
-
- PE0Tog();
- }
- void FSMC_init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
- FSMC_NORSRAMTimingInitTypeDef FSMC_NORSRAMTimingInitStructure;
- RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE); //使能FSMC時鐘
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE, ENABLE);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //復用推挽輸出
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- //PORTD復用推挽輸出
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_FSMC); //AD2
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_FSMC); //AD3
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource4, GPIO_AF_FSMC); //NOE
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_FSMC); //NWE
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_FSMC); //AD13
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_FSMC); //AD14
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource10, GPIO_AF_FSMC); //AD15
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource11, GPIO_AF_FSMC); //A16
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_FSMC); //A17
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_FSMC); //A18
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_FSMC); //AD0
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_FSMC); //AD1
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 |
- GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_9 |
- GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 |
- GPIO_Pin_13| GPIO_Pin_14 | GPIO_Pin_15;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
-
- //PORTE復用推挽輸出
- GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_FSMC); //AD4
- GPIO_PinAFConfig(GPIOE, GPIO_PinSource8, GPIO_AF_FSMC); //AD5
- GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_FSMC); //AD6
- GPIO_PinAFConfig(GPIOE, GPIO_PinSource10, GPIO_AF_FSMC); //AD7
- GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_FSMC); //AD8
- GPIO_PinAFConfig(GPIOE, GPIO_PinSource12, GPIO_AF_FSMC); //AD9
- GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_FSMC); //AD10
- GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_FSMC); //AD11
- GPIO_PinAFConfig(GPIOE, GPIO_PinSource15, GPIO_AF_FSMC); //AD12
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 |
- GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 |
- GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
- GPIO_Init(GPIOE, &GPIO_InitStructure);
- //PORTB復用推挽輸出
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_FSMC); //NDAV
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- FSMC_NORSRAMTimingInitStructure.FSMC_AddressSetupTime = 7;
- FSMC_NORSRAMTimingInitStructure.FSMC_AddressHoldTime = 4;
- FSMC_NORSRAMTimingInitStructure.FSMC_DataSetupTime = 10;
- FSMC_NORSRAMTimingInitStructure.FSMC_BusTurnAroundDuration = 0;
- FSMC_NORSRAMTimingInitStructure.FSMC_CLKDivision = 0;
- FSMC_NORSRAMTimingInitStructure.FSMC_DataLatency = 0;
- FSMC_NORSRAMTimingInitStructure.FSMC_AccessMode = FSMC_AccessMode_B;
- FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
- FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Enable;
- FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR;
- FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
- FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
- FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
- FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
- FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
- FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
- FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
- FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
- FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
- FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
- FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &FSMC_NORSRAMTimingInitStructure;
- FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
- /* Enable FSMC Bank1_SRAM Bank */
- FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
- }
- void GPIO_Configuration(void)
- {
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能GPIOA時鐘
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); //使能GPIOC時鐘
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); //使能GPIOE時鐘
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //PE0推拉輸出
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_Init(GPIOE, &GPIO_InitStructure);
-
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //PE3和PE2推拉輸出輸出
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_2; //
- GPIO_Init(GPIOE, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //PE3和PE2推拉輸出輸出
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_0; //
- GPIO_Init(GPIOC, &GPIO_InitStructure);
-
-
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_MCO);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //PA8推拉輸出輸出
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-
- }
- void EXTI_init(void)
- {
-
-
- EXTI_InitTypeDef EXTI_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //Enable GPIOB clock
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);// Enable SYSCFG clock
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
-
- SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource0);/* Connect EXTI Line0 to PB pin */
-
- EXTI_InitStructure.EXTI_Line = EXTI_Line0;/* Configure EXTI Line0 */
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure);
- EXTI_ClearFlag(EXTI_Line0);
- /* Enable and set EXTI Line0 Interrupt to the lowest priority */
- NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- keycode = 0;
- keysign = 0;
- }
- void TIM1_init(void)
- {
- /* TIM1 Configuration */
- /*定時時間TOUT=(124+1)*(167+1)/168=125us*/
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); //使能TIM1
- TIM_DeInit(TIM1); //復位定時器1
- TIM_TimeBaseStructure.TIM_Period=124; //設置自動重裝載寄存器周期的值
- TIM_TimeBaseStructure.TIM_Prescaler=167; //設置時鐘頻率除數的預分頻值
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //設置時鐘分割TIM_CKD_DIV1=0x0000,
- TIM_TimeBaseStructure.TIM_RepetitionCounter=0x00; //設置RCR寄存器值(這個只有高級定時器中有)
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //TIIM向上計數
- TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure); //初始化TIM3
- TIM_Cmd(TIM1,ENABLE); //開啟TIM1計數
- TIM_ClearFlag(TIM1, TIM_FLAG_Update); //清溢出標志
- }
- void TIM2_init(void)
- {
- /*我估計SYSCLK為168MHz,AHB的預分頻器為1,APB1預分頻器為4,因此APB1的時鐘頻率為42MHz,TIM2的時鐘為84MHz*/
- /* 定時時間TOUT=(610+1)*(167+1)/84=1000us */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能TIM2
- TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); //TIM2 Configuration
- TIM_TimeBaseStructure.TIM_Period =499; //
- TIM_TimeBaseStructure.TIM_Prescaler = 167;
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
- /* TIM2 TRGO selection */
- TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); //更新事件被選為觸發輸入
-
- /* TIM2 enable counter */
- TIM_Cmd(TIM2, ENABLE);
- }
- void TIM1INT_init(void)
- /* Enable the TIM1 global Interrupt */
- {
- NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE); //允許溢出中斷
- }
- void TIM2INT_init(void)
- /* Enable the TIM2 global Interrupt */
- {
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //允許溢出中斷
- }
- void DAC_init(void)
- {
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); //使能DAC時鐘
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; //PA.4 DAC1輸出;PA.5DAC2輸出
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
-
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- /* DAC channel1 Configuration */
- DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
- DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle;
- DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_2047;
- DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
- DAC_Init(DAC_Channel_1, &DAC_InitStructure);
- /* DAC channel2 Configuration */
-
-
- DAC_InitStructure.DAC_Trigger = DAC_Trigger_None; //不使用觸發功能TEN1=0
- DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; //不使用波形發生
- DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
- //DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; //DAC1輸出緩存關閉
- DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
- DAC_Init(DAC_Channel_2, &DAC_InitStructure); //初始化DAC通道1
- /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is
- automatically connected to the DAC converter. */
- DAC_Cmd(DAC_Channel_1, ENABLE);
- /* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is
- automatically connected to the DAC converter. */
- DAC_Cmd(DAC_Channel_2, ENABLE);
- /* Set DAC dual channel DHR12RD register */
- DAC_SetDualChannelData(DAC_Align_12b_R, 0x100, 0x100);
- }
- /*
- void ADC_init(void)
- {
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE);
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE);
-
- ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
- ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
- ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
- ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; //84/4=21MHz
- ADC_CommonInit(&ADC_CommonInitStructure);
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
簡易電子秤.zip
(569.63 KB, 下載次數: 23)
2018-7-18 20:04 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|