标题:
stm32f4简易电子秤程序
[打印本页]
作者:
foxfox12138
时间:
2018-7-18 20:04
标题:
stm32f4简易电子秤程序
简易电子秤程序,在此分享,欢迎下载
单片机源程序如下:
#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
作者:
驱蚊器若
时间:
2019-1-21 20:38
楼主能QQ分享一下资料吗,黑币不够无法下载
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1