今天捣鼓了一下AD采集,用段式液晶显示。
原理图:
看数据手册:
可以看出P0.9属于ADC通道4.
代码:
主程序:
#include "lks32mc03x_lib.h"
#include "hardware_init.h"
#include "delay.h"
/*******************************************************************************
º¯ÊýÃû³Æ£º int main(void)
¹¦ÄÜÃèÊö£º Ö÷³ÌÐòÈë¿Ú
ÊäÈë²ÎÊý£º ÎÞ
Êä³ö²ÎÊý£º ÎÞ
·µ »Ø Öµ£º ÎÞ
ÆäËü˵Ã÷£º ±¾ÊµÑéʹÓÃMCPWMµÄT0ʼþ×÷Ϊ´¥·¢ADCµÚÒ»¶Î²ÉÑùµÄ²ÉÑùʼþ£¬Ê¹ÓÃADC_CH5¼´ADC
ͨµÀ5²É¼¯µçλÆ÷µçѹ¡£
ÔËÐÐЧ¹û£º ³ÌÐòÉÕ¼ÁèŸLKS32MC033H6P8×îСϵͳ°å£¬KEIL5½øÈëdebugģʽ´ò¿ªWatch2´°¿Ú
²é¿´ADCDataVolt_Samp0 ADC²É¼¯µçλÆ÷ÊýÖµºÍVoltValue_Samp0µçλÆ÷ʵ¼Êµçѹ
Öµµ¥Î»V¡£
ÐÞ¸ÄÈÕÆÚ °æ±¾ºÅ ÐÞ¸ÄÈË ÐÞ¸ÄÄÚÈÝ
-----------------------------------------------------------------------------
2022/4/14 V1.0 HuangMG ´´½¨
*******************************************************************************/
s16 ADCDataVolt_Samp0 = 0;//ADC²É¼¯µçλÆ÷ÊýÖµ
float VoltValue_Samp0 = 0;//µçλÆ÷ʵ¼Êµçѹֵ
unsigned int VoltValue_Display=0.0;
#define uchar unsigned char
#define uint unsigned int
#define CS_0 GPIO_ResetBits (GPIO0, GPIO_Pin_6)
#define CS_1 GPIO_SetBits(GPIO0, GPIO_Pin_6)
#define WR_0 GPIO_ResetBits(GPIO0, GPIO_Pin_5)
#define WR_1 GPIO_SetBits(GPIO0, GPIO_Pin_5)
#define DA_0 GPIO_ResetBits(GPIO0, GPIO_Pin_4)
#define DA_1 GPIO_SetBits(GPIO0, GPIO_Pin_4)
uchar tmp;
uchar n1, n2, n3, n4;
unsigned char Ht1621Tab[]={0x00,0x00,0x00,0x00};
//unsigned char DispTab[]={0x7B,0x12,0x67,0x57,0x1E,0x5D,0x7D,0x13,0x7F,0x5F,0x69,0x45,0x3f, 0x7a, 0x3e};
// 0 1 2 3 4 5 6 7 8 9 C ? A V H
unsigned char DispTab[]={0x7B,0x12,0x67,0x57,0x1E,0x5D,0x7D,0x13,0x7F,0x5F,0x3F,0x7C,0x69,0x76,0x6D, 0x2D};
// 0 1 2 3 4 5 6 7 8 9 A b C d E F
#define BIAS 0x52 //0b1000 0101 0010 1/3duty 4com
#define SYSDIS 0X00 //0b1000 0000 0000 ???????LCD?????
#define SYSEN 0X02 //0b1000 0000 0010 ???????
#define LCDOFF 0X04 //0b1000 0000 0100 ?LCD??
#define LCDON 0X06 //0b1000 0000 0110 ??LCD??
#define XTAL 0x28 //0b1000 0010 1000 ?????
#define RC256 0X30 //0b1000 0011 0000 ????
#define WDTDIS1 0X0A //0b1000 0000 1010 ?????
void Ht1621_CS_0() { CS_0;} // define P2.2 ---> CS
void Ht1621_CS_1() { CS_1;}
void Ht1621_WR_0() { WR_0;} // define P2.1 ---> WR
void Ht1621_WR_1() { WR_1;}
void Ht1621_DO_0() { DA_0;} // define P2.0 ---> DAT
void Ht1621_DO_1() { DA_1;}
unsigned int counter;
void BSP_Configuration(void);
//-----------------------------------------------------------------------------------------
//????:Delay()
//? ?:?????
//-----------------------------------------------------------------------------------------
void Delay(unsigned char us) //5,7,9
{
while(--us);
}
//-----------------------------------------------------------------------------------------
//????:Delayms()
//-----------------------------------------------------------------------------------------
void Delayms(unsigned int ims)
{
unsigned int i,j;
for(i=0;i<ims;i++)
for(j=0;j<65;j++) { Delay(1); }
}
//-----------------------------------------------------------------------------------------
//Ht1621Wr_Data()
//-----------------------------------------------------------------------------------------
void Ht1621Wr_Data(unsigned char Data,unsigned char cnt)
{
unsigned char i;
for (i=0;i<cnt;i++)
{
Ht1621_WR_0();
Delay(10);
if((Data & 0x80)==0x80) {Ht1621_DO_1();Delay(100);}
else {Ht1621_DO_0();Delay(100);}
Ht1621_WR_1();
Delay(10);
Data<<=1;
}
}
//-----------------------------------------------------------------------------------------
//void Ht1621WrCmd(uchar Cmd)
//-----------------------------------------------------------------------------------------
void Ht1621WrCmd(unsigned char Cmd)
{
Ht1621_CS_0();
Delay(10);
Ht1621Wr_Data(0x80,4); //??????100
Ht1621Wr_Data(Cmd,8); //??????
Ht1621_CS_1();
Delay(10);
}
//-----------------------------------------------------------------------------------------
//void Ht1621WrOneData(uchar Addr,uchar Data)
//-----------------------------------------------------------------------------------------
void Ht1621WrOneData(unsigned char Addr,unsigned char Data)
{
Ht1621_CS_0();
Ht1621Wr_Data(0xa0,3); //??????101
Ht1621Wr_Data(Addr<<2,6); //??????
Ht1621Wr_Data(Data,4); //???????? 7 6 5 4
Ht1621_CS_1();
}
//-----------------------------------------------------------------------------------------
//void Ht1621WrAllData()
//-----------------------------------------------------------------------------------------
void Ht1621WrAllData(unsigned char Addr,unsigned char *p,unsigned char cnt)
{
unsigned char i;
Ht1621_CS_0();
Ht1621Wr_Data(0xa0,3); //??????101
Ht1621Wr_Data(Addr<<2,6); //??????
for (i=0;i<cnt;i++)
{
Ht1621Wr_Data(*p,8); //????
p++;
}
Ht1621_CS_1();
}
//-----------------------------------------------------------------------------------------
//void Ht1621_Init(void)
//-----------------------------------------------------------------------------------------
void Ht1621_Init(void)
{
Ht1621WrCmd(BIAS);
Ht1621WrCmd(RC256); //???????
//Ht1621WrCmd(XTAL); //???????
Ht1621WrCmd(SYSDIS);
Ht1621WrCmd(WDTDIS1);
Ht1621WrCmd(SYSEN);
Ht1621WrCmd(LCDON);
}
//-----------------------------------------------------------------------------------------
//Display_lcd_dot
//-----------------------------------------------------------------------------------------
void Display_lcd_dot(void)
{
Ht1621WrOneData(7 , 0x50);
}
//-----------------------------------------------------------------------------------------
//DISPLAY
//-----------------------------------------------------------------------------------------
void Display(void)
{
unsigned char com;
com = 0;
if((DispTab[n1]&0x01) == 0x01){ com = com + 0x10;}
if((DispTab[n2]&0x01) == 0x01){ com = com + 0x20;}
if((DispTab[n3]&0x01) == 0x01){ com = com + 0x40;}
if((DispTab[n4]&0x01) == 0x01){ com = com + 0x80;}
Ht1621WrOneData(0 , com);
com = 0;
if((DispTab[n1]&0x02) == 0x02){ com = com + 0x10;}
if((DispTab[n2]&0x02) == 0x02){ com = com + 0x20;}
if((DispTab[n3]&0x02) == 0x02){ com = com + 0x40;}
if((DispTab[n4]&0x02) == 0x02){ com = com + 0x80;}
Ht1621WrOneData(1 , com);
Display_lcd_dot();
com = 0;
if((DispTab[n1]&0x10) == 0x10){ com = com + 0x10;}
if((DispTab[n2]&0x10) == 0x10){ com = com + 0x20;}
if((DispTab[n3]&0x10) == 0x10){ com = com + 0x40;}
if((DispTab[n4]&0x10) == 0x10){ com = com + 0x80;}
Ht1621WrOneData(2 , com);
com = 0;
if((DispTab[n1]&0x40) == 0x40){ com = com + 0x10;}
if((DispTab[n2]&0x40) == 0x40){ com = com + 0x20;}
if((DispTab[n3]&0x40) == 0x40){ com = com + 0x40;}
if((DispTab[n4]&0x40) == 0x40){ com = com + 0x80;}
Ht1621WrOneData(3 , com);
com = 0;
if((DispTab[n1]&0x20) == 0x20){ com = com + 0x10;}
if((DispTab[n2]&0x20) == 0x20){ com = com + 0x20;}
if((DispTab[n3]&0x20) == 0x20){ com = com + 0x40;}
if((DispTab[n4]&0x20) == 0x20){ com = com + 0x80;}
Ht1621WrOneData(4 , com);
com = 0;
if((DispTab[n1]&0x08) == 0x08){ com = com + 0x10;}
if((DispTab[n2]&0x08) == 0x08){ com = com + 0x20;}
if((DispTab[n3]&0x08) == 0x08){ com = com + 0x40;}
if((DispTab[n4]&0x08) == 0x08){ com = com + 0x80;}
Ht1621WrOneData(5 , com);
com = 0;
if((DispTab[n1]&0x04) == 0x04){ com = com + 0x10;}
if((DispTab[n2]&0x04) == 0x04){ com = com + 0x20;}
if((DispTab[n3]&0x04) == 0x04){ com = com + 0x40;}
if((DispTab[n4]&0x04) == 0x04){ com = com + 0x80;}
Ht1621WrOneData(6 , com);
}
//-----------------------------------------------------------------------------------------
//convertor()
//-----------------------------------------------------------------------------------------
void data_convertor(uchar adc_value)
{
tmp=adc_value; //adc
n4=0;
n3=tmp/100;
n2=tmp%100/10;
// 4
n1=tmp%10; //display "C"
}
int main(void)
{
Hardware_init(); /* Ó²¼þ³õʼ»¯ */
Ht1621_Init(); //?????LCD
Delay(100); //??????
Ht1621WrAllData(0,Ht1621Tab,16);// Clear LCD display
Ht1621_Init();
for (;;)
{
ADCDataVolt_Samp0 = ADC_GetConversionValue(DAT0);/* »ñÈ¡µçλÆ÷²ÉÑùÊýÖµ */
VoltValue_Samp0 = (ADCDataVolt_Samp0*3.6 ) / 0x7ff0 ;/* µçλÆ÷²ÉÑùת»¯ÎªÄ£ÄâÁ¿ */
VoltValue_Display=(uchar)(VoltValue_Samp0*10000);
data_convertor(VoltValue_Display);
//data_convertor(235);
Display();
delay_ms(1000); /*ÑÓʱ1s*/
}
}
/************************ (C) COPYRIGHT LINKO SEMICONDUCTOR **********************/
/* ------------------------------END OF FILE------------------------------------ */
硬件初始化代码:
void Hardware_init(void)
{
__disable_irq(); /* ¹Ø±ÕÖÐ¶Ï ÖжÏ×Ü¿ª¹Ø */
SYS_WR_PROTECT = 0x7a83; /* ¿ªÆôдʹÄÜ*/
IWDG_DISABLE(); /* ¹Ø±Õ¶ÀÁ¢¿´ÃŹ·Ê¹ÄÜ*/
FLASH_CFG |= 0x00080000; /* FLASH Ԥȡ¼ÓËÙʹÄÜ*/
delay_init(48); /* ÑÓʱº¯Êý³õʼ»¯Ê±ÖÓ48MHz*/
GPIO_init(); /* GPIO³õʼ»¯ */
MCPWM_init(); /* MCPWM_init ³õʼ»¯*/
ADC0_init(); /* ADC0_init ³õʼ»¯ */
delay_us(100); /* ÑÓʱµÈ´ýÓ²¼þ³õʼ»¯Îȶ¨ */
__enable_irq(); /* ¿ªÆô×ÜÖÐ¶Ï */
}
void GPIO_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_StructInit(&GPIO_InitStruct);
/* P0.9 LED2*/
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; /*Êä³öģʽ*/
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; /*Pin9*/
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; /*ÉÏÀ¹¦ÄÜ*/
GPIO_Init(GPIO0, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; /*Êä³öģʽ*/
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; /*²»ÉÏÀ*/
GPIO_Init(GPIO0, &GPIO_InitStruct);
}
void ADC0_init(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_StructInit(&ADC_InitStructure); /*ADC³õʼ»¯½á¹¹Ìå*/
ADC_InitStructure.Align = ADC_LEFT_ALIGN; /* ADCÊý¾ÝÊä³ö×ó¶ÔÆë*/
ADC_InitStructure.Trigger_Mode = ADC_1SEG_TRG; /* ÉèÖÃADCת»»Ä£Ê½Îª4¶Îʽ²ÉÑù */
ADC_InitStructure.FirSeg_Ch = ADC_2_TIMES_SAMPLE;/* µÚÒ»¶Î¹²²ÉÑù2¸öͨµÀ */
ADC_InitStructure.SecSeg_Ch = 0; /* µÚ¶þ¶Î¹²²ÉÑù0¸öͨµÀ */
ADC_InitStructure.ThrSeg_Ch = 0; /* µÚÈý¶Î¹²²ÉÑù0¸öͨµÀ */
ADC_InitStructure.FouSeg_Ch = 0; /* µÚËĶι²²ÉÑù0¸öͨµÀ */
ADC_InitStructure.Trigger_Cnt = 0; /* µ¥¶Î´¥·¢Ä£Ê½Ï´¥·¢Ò»´Î²ÉÑùËùÐèÒªµÄʼþÊý:
0~15 0±íʾÐèÒªÒ»´Î´¥·¢£¬15±íʾÐèÒª16´Î*/
ADC_InitStructure.ADC_RANGE = ADC_RANGE_3V6; /* ADCÁ¿³ÌÑ¡Ôñ*/
ADC_InitStructure.Trigger_En = ADC_HARDWARE_T0_TRG; /* ¿ªÆôT0ºÍT1Ó²¼þ´¥·¢Ê¼þ */
ADC_InitStructure.SEL_En = ADC_MCPWM_SEL; /* MCPWM´¥·¢ADC²ÉÑù */
ADC_InitStructure.ADC_SAMP_CLK = 20; /* ÉèÖòÉÑùʱ¼äΪ20¸öADCʱÖÓÖÜÆÚ ·¶Î§4--35*/
ADC_InitStructure.IE = ADC_EOS0_IRQ_EN ; /* ¿ªÆôµÚÒ»¶ÎÖÐ¶Ï */
/* ADCÄ£Äâ¿´ÃŹ· */
ADC_InitStructure.ADC_GEN_En = DISABLE ; /* ¿´ÃŹ·¼à²â½ûÖ¹ */
ADC_InitStructure.ADC_GEN_HTH = 0; /* ADCÄ£Äâ¿´ÃŹ·ÉÏãÐÖµ */
ADC_InitStructure.ADC_GEN_LTH = 0; /* ADCÄ£Äâ¿´ÃŹ·ÏÂãÐÖµ */
ADC_InitStructure.REFOUT_EN = DISABLE; /* ADC REFÊä³öʹÄÜ*/
ADC_Init(ADC, &ADC_InitStructure);
ADC_ChannelConfig(ADC,CHN0, ADC_CHANNEL_4 ,ADC_CHANNEL_4, ADC_CHANNEL_4 ,ADC_CHANNEL_4);
ADC_ClearIRQFlag(ADC, ADC_ALL_IRQ_IF) ; /* ÇåÖжϱê־λ*/
}
void MCPWM_init(void)
{
MCPWM_InitTypeDef MCPWM_InitStructure;
MCPWM_StructInit(&MCPWM_InitStructure);
MCPWM_InitStructure.CLK_DIV = 0; /* MCPWMʱÖÓ·ÖƵÉèÖà */
MCPWM_InitStructure.MCLK_EN = ENABLE; /* Ä£¿éʱÖÓ¿ªÆô */
MCPWM_InitStructure.MCPWM_Cnt0_EN = ENABLE; /* ʱ»ù0Ö÷¼ÆÊýÆ÷¿ªÊ¼¼ÆÊýʹÄÜ¿ª¹Ø */
/* ×Ô¶¯¸üÐÂʹÄܼĴæÆ÷ MCPWM_TH00 ×Ô¶¯¼ÓÔØʹÄÜ MCPWM_TMR0 ×Ô¶¯¼ÓÔØʹÄÜ MCPWM_0TH ×Ô¶¯¼ÓÔØʹÄÜ MCPWM_0CNT ×Ô¶¯¼ÓÔØʹÄÜ*/
MCPWM_InitStructure.AUEN = TMR0_AUEN | TMR1_AUEN | TMR2_AUEN | TMR3_AUEN;
MCPWM_InitStructure.TimeBase0_PERIOD = PWM_PERIOD; /* ʱÆÚ0ÖÜÆÚÉèÖà */
MCPWM_InitStructure.TriggerPoint0 = (u16)( - (PWM_PERIOD>>2)); /* MCPWM_TMR0 ADC´¥·¢Ê¼þT0 ÉèÖà */
MCPWM_InitStructure.TriggerPoint1 = (u16)(-(PWM_PERIOD>>1));/* MCPWM_TMR1 ADC´¥·¢Ê¼þT1 ÉèÖà */
MCPWM_InitStructure.TriggerPoint2 = (u16)(PWM_PERIOD>>2); /* MCPWM_TMR0 ADC´¥·¢Ê¼þT0 ÉèÖà */
MCPWM_InitStructure.TriggerPoint3 = (u16)(PWM_PERIOD >> 1);/* MCPWM_TMR1 ADC´¥·¢Ê¼þT1 ÉèÖà */
MCPWM_InitStructure.CNT0_T0_Update_INT_EN = ENABLE; /* MCPWM T0ʼþ¸üÐÂʹÄÜ */
MCPWM_Init(MCPWM0, &MCPWM_InitStructure); /* MCPWM0 Ä£¿é³õʼ»¯ */
}
效果图:
2.11V.
2.28V.
1.58V.
1.75V.
1.93V.
|
共1人点赞
|