前言
**本程序设计是基于嵌入式开发板CT117E,stm32f103RBT6。
如果对哪个模块的代码不理解可以点开我的博客查看各个模块的编写思路。
一、试题
二、需要用到的模块
1.LED
代码如下:led.c:
#include "led.h"
void led_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC, ENABLE);
/* Configure PD0 and PD2 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = 0xff00;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIOC->ODR |=0xff<<8;
GPIOD->ODR |=1<<2;
GPIOD->ODR &=~(1<<2);
}
void led_ctrl(u8 ledx,u8 status) //控制led的亮灭,ledx取值范围:8-15
{
if(status)
{
GPIOC->ODR &=~(1<<ledx);
GPIOD->ODR |=1<<2;
GPIOD->ODR &=~(1<<2);
}
else
{
GPIOC->ODR |=1<<ledx;
GPIOD->ODR |=1<<2;
GPIOD->ODR &=~(1<<2);
}
}
led.h:
#ifndef LED_H
#define LED_H
#include "stm32f10x.h"
void led_init(void);
void led_ctrl(u8 ledx,u8 status);
#endif
2.按键
代码如下:key.c:
#include "key.h"
void key_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
/* Configure PD0 and PD2 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
key.h:
#include "key.h"
#ifndef KEY_H
#define KEY_H
#include "stm32f10x.h"
#define key1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) //读取按键的状态
#define key2 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8)
#define key3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)
#define key4 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8)
void key_init(void);
#endif
3.ADC
代码如下:adc.c:
#include "adc.h"
void adc_init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE);
/* Configure PC.01, PC.02 and PC.04 (ADC Channel11, Channel12 and Channel14)
as analog input ----------------------------------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
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);
/* ADC1 regular channels configuration */
//ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_239Cycles5);
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
}
u16 get_adc(u8 Channel)
{
u8 i,j;
u16 t[50];
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
ADC_RegularChannelConfig(ADC1, Channel, 1, ADC_SampleTime_239Cycles5);
for(i=0;i<50;i++)
{
t[i]=ADC_GetConversionValue (ADC1);
}
ADC_SoftwareStartConvCmd(ADC1, DISABLE);
for(i=0;i<50-1;i++)
{
for(j=0;j<50-1-i;j++)
{
if(t[j]>t[j+1])
{
t[j]=t[j] ^ t[j+1];
t[j+1]=t[j] ^ t[j+1];
t[j]=t[j] ^ t[j+1];
}
}
}
return (t[24]+t[25])/2;
}
}
adc.h:
#ifndef ADC_H
#define ADC_H
#include "stm32f10x.h"
void adc_init(void);
u16 get_adc(void);
#endif
4.定时器2两路捕获
代码如下:time2_capture.c:
#include "time2_capture.h"
void time2_capture_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* GPIOA clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* Enable the TIM3 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
/* TIM enable counter */
TIM_Cmd(TIM2, ENABLE);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM2, TIM_IT_CC2 | TIM_IT_CC3, ENABLE);
}
u32 ch2_val=0,ch2_duty=0;
u8 mode=1,ch2_mode=0;
u32 ch3_val=0,ch3_duty=0;
u8 ch3_mode=0;
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_CC2) == SET)
{
/* Clear TIM3 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
if(mode==1)
{
switch(ch2_mode)
{
case 0: ch2_val=0;
ch2_duty=0;
TIM_SetCounter(TIM2,0);
TIM_OC2PolarityConfig(TIM2,TIM_OCPolarity_Low);
ch2_mode=1;
break;
case 1: ch2_duty=TIM_GetCounter(TIM2);
TIM_OC2PolarityConfig(TIM2,TIM_OCPolarity_High);
ch2_mode=2;
break;
case 2: ch2_val=TIM_GetCounter(TIM2);
TIM_OC2PolarityConfig(TIM2,TIM_OCPolarity_High);
ch2_mode=3;
break;
default : break;
}
}
else
{
ch2_mode=0;
TIM_OC2PolarityConfig(TIM2,TIM_OCPolarity_High);
}
}
if(TIM_GetITStatus(TIM2, TIM_IT_CC3) == SET)
{
/* Clear TIM3 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
if(mode==0)
{
switch(ch3_mode)
{
case 0: ch3_val=0;
ch3_duty=0;
TIM_SetCounter(TIM2,0);
TIM_OC3PolarityConfig(TIM2,TIM_OCPolarity_Low);
ch3_mode=1;
break;
case 1: ch3_duty=TIM_GetCounter(TIM2);
TIM_OC3PolarityConfig(TIM2,TIM_OCPolarity_High);
ch3_mode=2;
break;
case 2: ch3_val=TIM_GetCounter(TIM2);
TIM_OC3PolarityConfig(TIM2,TIM_OCPolarity_High);
ch3_mode=3;
break;
default : break;
}
}
else
{
ch3_mode=0;
TIM_OC3PolarityConfig(TIM2,TIM_OCPolarity_High);
}
}
}
time2_capture.h:
#ifndef CAPTURE_H
#define CAPTURE_H
#include "stm32f10x.h"
extern u32 ch2_val,ch2_duty;
extern u8 mode,ch2_mode;
extern u32 ch3_val,ch3_duty;
extern u8 ch3_mode;
void time2_capture_init(void);
#endif
5.定时器3产生两路pwm
代码如下:pwm.c:
#include "pwm.h"
void time3_pwm_init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* GPIOA clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* Enable the TIM3 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
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);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* Output Compare Toggle Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 65535;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
/* TIM enable counter */
TIM_Cmd(TIM3, ENABLE);
/* TIM IT enable */
TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 , ENABLE);
}
u32 CH1_VAL,CH2_VAL,CH1_DUTY,CH2_DUTY;
void set_pwm(u32 ch1_val,u32 ch1_duty,u32 ch2_val,u32 ch2_duty)
{
CH1_VAL=1000000/ch1_val;
CH2_VAL=1000000/ch2_val;
CH1_DUTY=ch1_duty * CH1_VAL / 100;
CH2_DUTY=ch2_duty * CH2_VAL / 100;
TIM_SetCounter(TIM3,0);
TIM_SetCompare1(TIM3,0);
TIM_SetCompare2(TIM3,0);
}
u32 capture;
static u8 ch1_mode=0,ch2_mode=0;
void TIM3_IRQHandler(void)
{
/* TIM3_CH1 toggling with frequency = 183.1 Hz */
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1 );
capture = TIM_GetCapture1(TIM3);
if(ch1_mode)
{
TIM_SetCompare1(TIM3,capture + CH1_VAL - CH1_DUTY);
}
else
{
TIM_SetCompare1(TIM3,capture + CH1_DUTY);
}
ch1_mode ^=1;
}
if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2 );
capture = TIM_GetCapture2(TIM3);
if(ch2_mode)
{
TIM_SetCompare2(TIM3,capture + CH2_VAL - CH2_DUTY);
}
else
{
TIM_SetCompare2(TIM3,capture + CH2_DUTY);
}
ch2_mode ^=1;
}
}
pwm.h:
#ifndef PWM_H
#define PWM_H
#include "stm32f10x.h"
void set_pwm(u32 ch1_val,u32 ch1_duty,u32 ch2_val,u32 ch2_duty);
void time3_pwm_init(void);
#endif
6.i2c对eeprom的存取
i2c.c
/*
程序说明: CT117E嵌入式竞赛板GPIO模拟I2C总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT117E嵌入式竞赛板
日 期: 2011-8-9
*/
#include "stm32f10x.h"
#include "i2c.h"
/** I2C 总线接口 */
#define I2C_PORT GPIOB
#define SDA_Pin GPIO_Pin_7
#define SCL_Pin GPIO_Pin_6
#define FAILURE 0
#define SUCCESS 1
//配置SDA信号线为输入模式
void SDA_Input_Mode()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = SDA_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(I2C_PORT, &GPIO_InitStructure);
}
//配置SDA信号线为输出模式
void SDA_Output_Mode()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = SDA_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(I2C_PORT, &GPIO_InitStructure);
}
//
void SDA_Output( uint16_t val )
{
if ( val ) {
GPIO_SetBits(I2C_PORT,SDA_Pin);
} else {
GPIO_ResetBits(I2C_PORT,SDA_Pin);
}
}
//
void SCL_Output( uint16_t val )
{
if ( val ) {
GPIO_SetBits(I2C_PORT,SCL_Pin);
} else {
GPIO_ResetBits(I2C_PORT,SCL_Pin);
}
}
//
uint8_t SDA_Input()
{
return GPIO_ReadInputDataBit( I2C_PORT, SDA_Pin);
}
//延时程序
void delay1(unsigned int n)
{
unsigned int i;
for ( i=0;i<n;++i);
}
//I2C总线启动
void I2CStart(void)
{
SDA_Output(1);delay1(500);
SCL_Output(1);delay1(500);
SDA_Output(0);delay1(500);
SCL_Output(0);delay1(500);
}
//I2C总线停止
void I2CStop(void)
{
SCL_Output(0); delay1(500);
SDA_Output(0); delay1(500);
SCL_Output(1); delay1(500);
SDA_Output(1); delay1(500);
}
//等待应答
unsigned char I2CWaitAck(void)
{
unsigned short cErrTime = 5;
SDA_Input_Mode();
delay1(500);
SCL_Output(1);delay1(500);
while(SDA_Input())
{
cErrTime--;
delay1(500);
if (0 == cErrTime)
{
SDA_Output_Mode();
I2CStop();
return FAILURE;
}
}
SDA_Output_Mode();
SCL_Output(0);delay1(500);
return SUCCESS;
}
//发送应答位
void I2CSendAck(void)
{
SDA_Output(0);delay1(500);
delay1(500);
SCL_Output(1); delay1(500);
SCL_Output(0); delay1(500);
}
//
void I2CSendNotAck(void)
{
SDA_Output(1);
delay1(500);
SCL_Output(1); delay1(500);
SCL_Output(0); delay1(500);
}
//通过I2C总线发送一个字节数据
void I2CSendByte(unsigned char cSendByte)
{
unsigned char i = 8;
while (i--)
{
SCL_Output(0);delay1(500);
SDA_Output(cSendByte & 0x80); delay1(500);
cSendByte += cSendByte;
delay1(500);
SCL_Output(1);delay1(500);
}
SCL_Output(0);delay1(500);
}
//从I2C总线接收一个字节数据
unsigned char I2CReceiveByte(void)
{
unsigned char i = 8;
unsigned char cR_Byte = 0;
SDA_Input_Mode();
while (i--)
{
cR_Byte += cR_Byte;
SCL_Output(0);delay1(500);
delay1(500);
SCL_Output(1);delay1(500);
cR_Byte |= SDA_Input();
}
SCL_Output(0);delay1(500);
SDA_Output_Mode();
return cR_Byte;
}
//I2C总线初始化
void i2c_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = SDA_Pin | SCL_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // **
GPIO_Init(I2C_PORT, &GPIO_InitStructure);
}
void i2c_write(u8 addr,u8 data)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CSendByte(data);
I2CWaitAck();
I2CStop();
}
u8 i2c_read(u8 addr)
{
u8 t;
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
t=I2CReceiveByte();
I2CWaitAck();
I2CStop();
return t;
}
i2c.h
#ifndef __I2C_H__
#define __I2C_H__
void i2c_init(void);
void delay1(unsigned int n);
void I2CStart(void);
void I2CStop(void);
void I2CSendAck(void);
void I2CSendNotAck(void);
unsigned char I2CWaitAck(void);
void I2CSendByte(unsigned char cSendByte);
unsigned char I2CReceiveByte(void);
void i2c_write(u8 addr,u8 data);
u8 i2c_read(u8 addr);
#endif
三、主函数逻辑设计
stm32f10x_it.c:
#include "stm32f10x_it.h"
extern u32 TimingDelay;
extern u8 key_flag;
extern u8 mode;
void SysTick_Handler(void)
{
static u8 key_num=0;
static u16 time_num=0;
key_num++;
time_num++;
TimingDelay--;
if(key_num==50)
{
key_num=0;
key_flag=1;
}
if(time_num==200)
{
time_num=0;
mode ^=1;
}
}
main.c:
#include "stm32f10x.h"
#include "stdio.h"
#include "lcd.h"
#include "led.h"
#include "key.h"
#include "time2_capture.h"
#include "adc.h"
#include "pwm.h"
#include "i2c.h"
u32 TimingDelay = 0;
u8 key_flag=0; //每50ms刷新一下
u8 key1_num=0; /
u8 key2_num=0;
u8 key3_num=0;
u8 key4_num=0;
u8 buff[20];
u8 key1_flag=1;
u16 adc_val1=0,adc_val2=0;
u8 fp=1,bp=2;
u8 key2_flag=1;
void Delay_Ms(u32 nTime);
void key_read(void);
void show_lcd(void);
//Main Body
int main(void)
{
SysTick_Config(SystemCoreClock/1000);
Delay_Ms(200);
STM3210B_LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
led_init();
key_init();
time2_capture_init();
adc_init();
time3_pwm_init();
i2c_init();
if(i2c_read(0x15)!=58)
{
i2c_write(0x15,58);
Delay_Ms(5);
i2c_write(0x20,fp);
Delay_Ms(5);
i2c_write(0x21,bp);
Delay_Ms(5);
}
fp=i2c_read(0x20);
Delay_Ms(5);
bp=i2c_read(0x21);
Delay_Ms(5);
while(1)
{
if(key_flag)
{
key_read();
key_flag=0;
}
show_lcd();
}
}
//
void Delay_Ms(u32 nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
void key_read(void)
{
if(key1==0)
{
key1_num++;
if(key1_num>20)
{
}
}
else
{
if(key1_num>1 && key1_num<10)
{
key1_flag ^=1;
}
key1_num=0;
}
if(key2==0)
{
key2_num++;
if(key2_num>20)
{
}
}
else
{
if(key2_num>1 && key2_num<10)
{
key2_flag ^=1;
}
key2_num=0;
}
if(key3==0 && key1_flag==0)
{
key3_num++;
if(key3_num>20)
{
}
}
else
{
if(key3_num>1 && key3_num<10)
{
if(key2_flag==1)
{
if(fp<4)
fp++;
else
fp=4;
i2c_write(0x20,fp);
Delay_Ms(5);
}
else
{
if(bp<4)
bp++;
else
bp=4;
i2c_write(0x21,bp);
Delay_Ms(5);
}
}
key3_num=0;
}
if(key4==0 && key1_flag==0)
{
key4_num++;
if(key4_num>20)
{
}
}
else
{
if(key4_num>1 && key4_num<10)
{
if(key2_flag==1)
{
if(fp>1)
fp--;
else
fp=1;
i2c_write(0x20,fp);
Delay_Ms(5);
}
else
{
if(bp>1)
bp--;
else
bp=1;
i2c_write(0x21,bp);
Delay_Ms(5);
}
}
key4_num=0;
}
}
void show_lcd(void)
{
if(key1_flag)
{
led_ctrl(8,0);
LCD_SetBackColor(Black);
LCD_DisplayStringLine(Line1," Main ");
adc_val1=get_adc(ADC_Channel_4);
sprintf((char *)buff," AO1: %0.2fV ",(float)adc_val1/0xfff*3.3) ;
LCD_DisplayStringLine(Line5,buff);
adc_val2=get_adc(ADC_Channel_5);
sprintf((char *)buff," AO2: %0.2fV ",(float)adc_val2/0xfff*3.3) ;
LCD_DisplayStringLine(Line6,buff);
if((float)adc_val1/0xfff*3.3 >(float)adc_val2/0xfff*3.3 )
{
led_ctrl(15,1);
}
else
{
led_ctrl(15,0);
}
if(ch2_mode==3)
{
sprintf((char *)buff," PULS1: %dKHz ",1000000/ch2_val/1000) ;
LCD_DisplayStringLine(Line3,buff);
ch2_mode=0;
}
if(ch3_mode==3)
{
sprintf((char *)buff," PULS2: %dKHz ",1000000/ch3_val/1000) ;
LCD_DisplayStringLine(Line4,buff);
ch3_mode=0;
}
LCD_DisplayStringLine(Line8," 1 ");
set_pwm(1000000/ch2_val/fp,ch2_duty*100/ch2_val,1000000/ch3_val*bp,ch3_duty*100/ch3_val);
}
else
{
led_ctrl(8,1);
LCD_SetBackColor(Black);
LCD_DisplayStringLine(Line1," Setting ");
if(key2_flag==1)
{
LCD_SetBackColor(Blue);
}
else
{
LCD_SetBackColor(Black);
}
sprintf((char *)buff," FP_NUM: %d ",fp) ;
LCD_DisplayStringLine(Line3,buff);
if(key2_flag==0)
{
LCD_SetBackColor(Blue);
}
else
{
LCD_SetBackColor(Black);
}
sprintf((char *)buff," BP_NUM: %d ",bp) ;
LCD_DisplayStringLine(Line4,buff);
LCD_SetBackColor(Black);
LCD_DisplayStringLine(Line5," ");
LCD_DisplayStringLine(Line6," ");
LCD_DisplayStringLine(Line8," 2 ");
set_pwm(1000000/ch2_val/fp,0,1000000/ch3_val*bp,0);
}
}
四、 代码测试结果图
①主测试界面:
②设置界面