|
初学stm8l15x芯片,看了下网上的一些例程,改写了一个使用STM8L152K4芯片读取外部光敏电阻电压值,并使用芯片内部ad转换,将对应电压转换为光敏值,同时在高于2.0时触发高报继电器,低于1.0时触发低报继电器;
/*采集外部光敏电压输出值,通过内置12位ad转换显示出对应的光敏值,
高于x1触发继电器一,低于x2触发继电器二,
同时将采集到的光敏值通过lcd四位显示出来
2020-08-13
by 倪sir*/
#include "stm8l15x.h"
#include "lcd.h"
unsigned char Ht1621Tab[]={0x00,0x00,0x00,0x00};
//以下定义LED与按键的接口
#define LED1_GPIO_PORT GPIOC //BLUE
#define LED1_GPIO_PINS GPIO_Pin_1
#define LED2_GPIO_PORT GPIOC //RED
#define LED2_GPIO_PINS GPIO_Pin_0
/*不精确延时函数*/
void Delay(__IO uint16_t nCount)
{
/* Decrement nCount value */
while (nCount != 0)
{
nCount--;
}
}
/*ad的初始化函数*/
void ad_init()
{
CLK_PeripheralClockConfig (CLK_Peripheral_ADC1,ENABLE);
ADC_Init (ADC1,ADC_ConversionMode_Single,ADC_Resolution_12Bit,ADC_Prescaler_1);//ADC1,单次采样,12位,1分频
ADC_SamplingTimeConfig(ADC1,ADC_Group_FastChannels,ADC_SamplingTime_384Cycles);//采样周期设置
//ADC_VrefintCmd(ENABLE);//内部基准电压使能
ADC_ChannelCmd (ADC1,ADC_Channel_8,ENABLE);//adc1的8通道为pd6引脚
//ADC_ChannelCmd (ADC1,ADC_Channel_Vrefint,ENABLE);
ADC_Cmd(ADC1,ENABLE);//ADC1使能
}
void gpio_init()
{
GPIO_Init(LED1_GPIO_PORT, LED1_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);//初始化LED1,GPC4高速高电平推挽输出
GPIO_Init(LED2_GPIO_PORT, LED2_GPIO_PINS, GPIO_Mode_Out_PP_Low_Slow);//初始化LED2,GPC4高速高电平推挽输出
CLK_PeripheralClockConfig (CLK_Peripheral_TIM4,ENABLE); //使能外设时钟,STM8L默认所有外设时钟初始时关闭,使用前徐开启
}
int main( void )
{
u16 u16_adc1_value;
u16 VoltageValue;
InitLCD();
gpio_init();
ad_init();
while(1)
{
ADC_SoftwareStartConv (ADC1);//开启软件转换
while(!ADC_GetFlagStatus (ADC1,ADC_FLAG_EOC));//等待转换结束
ADC_ClearFlag (ADC1,ADC_FLAG_EOC);//清除相关标识
u16_adc1_value=ADC_GetConversionValue (ADC1);//获取转换值
VoltageValue=u16_adc1_value*3300UL/4095UL;
data_convertor(VoltageValue);
Display();
Display_lcd_dot();
// Delay(5000);
if(VoltageValue >=2000)
{
GPIO_SetBits(LED1_GPIO_PORT, LED1_GPIO_PINS);
GPIO_ResetBits(LED2_GPIO_PORT, LED2_GPIO_PINS);
}
if((VoltageValue<2000)&&(VoltageValue>1000))
{
GPIO_ResetBits(LED1_GPIO_PORT, LED1_GPIO_PINS);
GPIO_ResetBits(LED2_GPIO_PORT, LED2_GPIO_PINS);
}
if((VoltageValue<2000)&&(VoltageValue>1000))
{
GPIO_ResetBits(LED1_GPIO_PORT, LED1_GPIO_PINS);
GPIO_SetBits(LED2_GPIO_PORT, LED2_GPIO_PINS);
}
}
Delay(5000);
Delay(5000);
Delay(5000);
Delay(5000);
Delay(5000);
Delay(5000);
Delay(5000);
Delay(5000);
Delay(5000);
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
```
lcd显示函数
```c
#include "stm8l15x.h"
#include "lcd.h"
uint8_t LCD_DisBuffer[4];//显示缓冲区
//码值表 /* 0 1 2 3 4 */
__CONST uint16_t NumberMap[10] = {0x3132, 0x2020, 0x3212, 0x3222, 0x2320,
/* 5 6 7 8 9 */
0x1322, 0x1332, 0x3020, 0x3332, 0x3322};
//数值 BAGFCEDP 码值
//0 11011110 3132
//1 10001000 2020
//2 11100110 3212
//3 11101010 3222
unsigned int tmp,n1, n2, n3, n4;
void InitLCD(void)
{
CLK_PeripheralClockConfig(CLK_Peripheral_LCD, ENABLE);//LCD驱动时钟门控使能
CLK_RTCClockConfig(CLK_RTCCLKSource_LSI, CLK_RTCCLKDiv_1);//配置RTC LSI或LSE
LCD_DeInit( );//LCD相关寄存器恢复初值
LCD_Init(LCD_Prescaler_4, LCD_Divider_16,//LCD驱动初始化,LCD刷新频率
LCD_Duty_1_4, LCD_Bias_1_3,//1/4DUTY 1/3BIAS,看屏幕参数
LCD_VoltageSource_External);//选择LCD驱动电源,外部或内部
LCD_PortMaskConfig(LCD_PortMaskRegister_0, 0x00); //SEG0-7,STM8L152K4T6没有
LCD_PortMaskConfig(LCD_PortMaskRegister_1, 0xFF); //SEG8-SEG15,STM8L参考手册228页
LCD_PortMaskConfig(LCD_PortMaskRegister_2, 0x00); //以下SEG均没选用
LCD_PortMaskConfig(LCD_PortMaskRegister_3, 0x00);
LCD_PortMaskConfig(LCD_PortMaskRegister_4, 0x00);
LCD_PortMaskConfig(LCD_PortMaskRegister_5, 0x00);
LCD_ContrastConfig(LCD_Contrast_3V3);//配置对比度
LCD_DeadTimeConfig(LCD_DeadTime_0); //设置死区时间
LCD_PulseOnDurationConfig(LCD_PulseOnDuration_5);// 配置LCD脉冲持续时间
LCD_HighDriveCmd(ENABLE);
LCD_Cmd(ENABLE);
}
/*数据转换函数*/
void data_convertor(unsigned long adc_value)
{
tmp=adc_value; //adc
n4=tmp/1000;
tmp=tmp%1000;
n3=tmp/100;
tmp=tmp%100;
n2=tmp/10;
tmp=tmp%10;
n1=tmp;
}
void Display()
{ //对显示缓冲区操作,要结合液晶说明书
LCD_DisBuffer[0]=(((NumberMap[n4] &0xf000)>>12)<<0)|(((NumberMap[n3] &0xf000)>>12)<<2)|(((NumberMap[n2] &0xf000)>>12)<<4)|(((NumberMap[n1] &0xf000)>>12)<<6);//COM0,SEG15-SEG8
LCD_DisBuffer[1]=(((NumberMap[n4] &0x0f00)>>8)<<0)|(((NumberMap[n3] &0x0f00)>>8)<<2)|(((NumberMap[n2] &0x0f00)>>8)<<4)|(((NumberMap[n1] &0x0f00)>>8)<<6); //COM1
LCD_DisBuffer[2]=(((NumberMap[n4] &0x00f0)>>4)<<0)|(((NumberMap[n3] &0x00f0)>>4)<<2)|(((NumberMap[n2] &0x00f0)>>4)<<4)|(((NumberMap[n1] &0x00f0)>>4)<<6); //COM2
LCD_DisBuffer[3]=(((NumberMap[n4] &0x000f)>>0)<<0)|(((NumberMap[n3] &0x000f)>>0)<<2)|(((NumberMap[n2] &0x000f)>>0)<<4)|(((NumberMap[n1] &0x000f)>>0)<<6); //COM3
LCD_WriteRAM(LCD_RAMRegister_1,LCD_DisBuffer[0]); //SEG8-SEG15对应寄存器RAM1,COM0
LCD_WriteRAM(LCD_RAMRegister_4,LCD_DisBuffer[1]<<4);//COM1,SEG8-SEG11对应RAM4高四位
LCD_WriteRAM(LCD_RAMRegister_5,LCD_DisBuffer[1]>>4);//COM1,SEG12-SEG15对应RAM5低四位
LCD_WriteRAM(LCD_RAMRegister_8,LCD_DisBuffer[2]); //COM2,SEG8-SEG15对应RAM8
LCD_WriteRAM(LCD_RAMRegister_11,LCD_DisBuffer[3]<<4);//COM3,SEG8-SEG11对应RAM11高四位
LCD_WriteRAM(LCD_RAMRegister_12,LCD_DisBuffer[3]>>4);//COM3,SEG12-SEG15对应RAM12低四位
}
void Display_lcd_dot(void)
{
LCD_WriteRAM(LCD_RAMRegister_11,(LCD_DisBuffer[3]<<4)|0x40);//2P,COM3,SEG10对应RAM的第7位,所以要与0X40相或
// LCD_WriteRAM(LCD_RAMRegister_12,LCD_DisBuffer[3]>>4|0x04; //4P
//LCD_WriteRAM(LCD_RAMRegister_12,LCD_DisBuffer[3]>>4|0x01;//3P
}
```
希望自己早些入门,敬上!!
有需要stm8l15x库函数,自取!
|
评分
-
查看全部评分
|