stm32内部AD采样程序_第1页
stm32内部AD采样程序_第2页
stm32内部AD采样程序_第3页
stm32内部AD采样程序_第4页
stm32内部AD采样程序_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/stm32内部AD采样程序。运行通过的请大家放心使用。/有缺陷的地方请大家多多指教。本人才刚刚起步,还有劳各位大侠带一把呀!/* Includes -*/#include stm32f10x_lib.h#includestdio.h/* Private typedef -*/* Private define -*/#define ADC1_DR_Address (u32)0x4001244C)int AD_value;/* Private macro -*/* Private variables -*/ADC_InitTypeDef ADC_InitStructure;vu16 ADC_ConvertedValue;ErrorStatus HSEStartUpStatus;/* Private function prototypes -*/void RCC_Configuration(void);void GPIO_Configuration(void);void SysTick_Configuration(void);void SetupClock (void);#define LED1ON GPIO_SetBits(GPIOF,GPIO_Pin_0)#define LED1OFF GPIO_ResetBits(GPIOF,GPIO_Pin_0) /片选cs#define LED2ON GPIO_SetBits(GPIOF,GPIO_Pin_1)#define LED2OFF GPIO_ResetBits(GPIOF,GPIO_Pin_1)/复位rest#define LED3ON GPIO_SetBits(GPIOF,GPIO_Pin_2)#define LED3OFF GPIO_ResetBits(GPIOF,GPIO_Pin_2)/数据sdata#define LED4ON GPIO_SetBits(GPIOF,GPIO_Pin_3)#define LED4OFF GPIO_ResetBits(GPIOF,GPIO_Pin_3)/时钟sclkunsigned char Ver=A; /驱动版本,默认为A版本,一共4个版本void delayms(unsigned int ii)/1ms延时函数unsigned int i,x;for (x=0;xii;x+)for (i=0;i100;i+);void send(unsigned char value, unsigned char cd) /写8位数据unsigned char i,dt;LED1OFF;LED4OFF;if(cd = 0)LED3OFF;elseLED3ON;LED4ON;dt=value;for(i=0;i8;i+)LED4OFF;if(dt&0x80)LED3ON;elseLED3OFF;dt=dt1;LED4ON;LED1ON;/cs=1;void readdata() /读数据unsigned char i,j;unsigned char ch6;send(0x09, 0); / 读数据LED1OFF;/cs=0;LED4OFF;/sclk=0;for (i=0;i6;i+)chi=0;for (j=0;j8;j+)LED4ON;/sclk=1;if (!(GPIO_ReadOutputDataBit(GPIOF,GPIO_Pin_2)/sdata=0)chi=chi|(1(7-j);LED4OFF;/sclk=0;switch(ch0)case 255:Ver=A;break;case 127:Ver=B;break;case 0:if(ch1+ch2+ch3=0)Ver=D;elseVer=C;break;LED1ON;/cs=1;void LCD_Initialize() /LCD初始化unsigned char i;LED2ON;/rest=1;LED1ON;/cs=1;LED3OFF;/sdata=0;LED4ON;/sclk=1;LED2OFF;/rest=0;delayms(50);LED2ON;/rest=1;delayms(50);send(0x00, 0); / NOPdelayms(5);send(0x01, 0); / LCD Software Resetdelayms(5);send(0xC6, 0); / Initial Escapesend(0xB9, 0); / Refresh setsend(0x00, 1);readdata(); /读出数据send(0xB5, 0); / Gamma Curve Setsend(0x01, 1);send(0xbd, 0); /common driver output select/很重要的if(Ver=D)send(0x04, 1);elsesend(0x00, 1);send(0xBE, 0); / Power Controlsend(0x03, 1);send(0x11, 0); / Sleep outsend(0xBA, 0); / Power Controlsend(0x7F, 1);send(0x03, 1);send(0x36, 0); / 扫描方式if (Ver=A)send(0x00|0x08,1); /RGBelsesend(0x00,1); /RGBsend(0xB7, 0); / Temperature gradient setfor(i=0; i14; i+)send(0, 1);send(0x29, 0); /display ONsend(0x03, 0); / Booster Voltage ONdelayms(20); / Booster Voltage stabilisieren lassensend(0x20, 0); /display inversion OFFsend(0x25, 0); / Write contrastswitch(Ver) /对比度设置case A:send(70, 1); /对比度设置break;case B:send(67, 1); /对比度设置break;case C:/send(74, 1);send(66, 1);/ send(64, 1); /对比度设置break;case D: /对比度设置send(39, 1);break;/*/颜色模式设置:color=1为4096色模式/ :color=0为256色模式/*void LCD_ColorSet(unsigned char Color)if (Color=1) send(0x3a, 0); /interface pixel formatsend(0x03, 1); /0x03 为4096色,0x02为256色elsesend(0x3a, 0); /interface pixel formatsend(0x02, 1); /0x03 为4096色,0x02为256色send(0x2d, 0); /调色板设置if (Ver=B|Ver=C)/redsend(0x00, 1);send(0x02, 1);send(0x03, 1);send(0x04, 1);send(0x05, 1);send(0x06, 1);send(0x08, 1);send(0x0f, 1);/greensend(0x00, 1);send(0x02, 1);send(0x03, 1);send(0x04, 1);send(0x05, 1);send(0x06, 1);send(0x08, 1);send(0x0f, 1);/bluesend(0x00, 1);send(0x03, 1);send(0x06, 1);send(0x0f, 1);else/redsend(0x00, 1);send(0x02, 1);send(0x03, 1);send(0x04, 1);send(0x05, 1);send(0x06, 1);send(0x08, 1);send(0x0f, 1);/greensend(0x00, 1);send(0x02, 1);send(0x03, 1);send(0x04, 1);send(0x05, 1);send(0x06, 1);send(0x08, 1);send(0x0f, 1);/bluesend(0x00, 1);send(0x03, 1);send(0x06, 1);send(0x0f, 1);void addset(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2) /坐标设置 4个参数依次为开始X,开始Y,结束x,结束Ysend(0x2a,0 );/column address setsend(x1,1 );send(x2,1 );send(0x2B,0 );/page address setsend(y1,1 );send(y2,1 );send(0x2C,0 );/memory write/*/清屏:color=1为4096色模式清屏/ :color=0为256色模式清屏/*void LCD_Clear(unsigned int value,unsigned char Color)unsigned char x, y;addset(0,0,97,66);if (Color=1)for(y = 0; y 67; y +)for(x = 0; x 4,1);send(value&0x0f)8),1);send(value,1);elsefor(y = 0; y 67; y +)for(x = 0; x 98; x +)send(value,1);/在指定位置显示一个字符(8*12大小)/dcolor为内容颜色,gbcolor为背静颜色void showdian(unsigned char x,unsigned char y,unsigned char dcolor,unsigned char bgcolor)addset(x,y,x+1,y+1); /设置区域send(dcolor,1);send(bgcolor,1);int main(void)/int i,j;#ifdef DEBUGdebug();#endif/* System clocks configuration -*/RCC_Configuration();SysTick_Configuration();/* GPIO configuration -*/GPIO_Configuration();/printf(rn USART1 print AD_value - rn);/* DMA channel1 configuration -*/*DMA_DeInit(DMA1_Channel1);DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;DMA_InitStructure.DMA_BufferSize = 1;DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;DMA_InitStructure.DMA_Priority = DMA_Priority_High;DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;DMA_Init(DMA1_Channel1, &DMA_InitStructure);/ Enable DMA channel1DMA_Cmd(DMA1_Channel1, ENABLE);*/* 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 channel13 configuration */ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 1, ADC_SampleTime_239Cycles5);/* Enable ADC1 DMA */ADC_DMACmd(ADC1, ENABLE);/* Enable ADC1 */ADC_Cmd(ADC1, ENABLE);/* Enable ADC1 reset calibaration register */ADC_ResetCalibration(ADC1);/* Check the end of ADC1 reset calibration register */while(ADC_GetResetCalibrationStatus(ADC1);/* Start ADC1 calibaration */ADC_StartCalibration(ADC1);/* Check the end of ADC1 calibration */while(ADC_GetCalibrationStatus(ADC1);/* Start ADC1 Software Conversion */ADC_SoftwareStartConvCmd(ADC1, ENABLE);LCD_Initialize() ;LCD_ColorSet(0); /0为256色模式,1为4096色模式LCD_Clear(255,0); /清屏addset(0,0,103,81); /设置起止坐标,4个参数依次为开始X,开始Y,结束x,结束Ywhile(1)AD_value=ADC_GetConversionValue(ADC1);/*for(i=0;i100;i+)for(j=0;j20;j+)AD_value=ADC_GetConversionValue(ADC1);/ Printf message with AD value to serial port every 1 secondshowdian(i,65-AD_value/63 ,0x03,255);delayms(100);LCD_Clear(255,0); /清屏addset(0,0,103,81);*/* Function Name : RCC_Configuration* Description : Configures the different system clocks.* Input : None* Output : None* Return : None*/void RCC_Configuration(void) /* RCC system reset(for debug purpose) */RCC_DeInit();/* Enable HSE */RCC_HSEConfig(RCC_HSE_ON);/* Wait till HSE is ready */HSEStartUpStatus = RCC_WaitForHSEStartUp();if(HSEStartUpStatus = SUCCESS)/* HCLK = SYSCLK */RCC_HCLKConfig(RCC_SYSCLK_Div1);/* PCLK2 = HCLK */RCC_PCLK2Config(RCC_HCLK_Div1);/* PCLK1 = HCLK/2 */RCC_PCLK1Config(RCC_HCLK_Div2);/* ADCCLK = PCLK2/4 */RCC_ADCCLKConfig(RCC_PCLK2_Div8);/* Flash 2 wait state */FLASH_SetLatency(FLASH_Latency_2);/* Enable Prefetch Buffer */FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);/* PLLCLK = 8MHz * 9 = 56 MHz */RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);/* Enable PLL */RCC_PLLCmd(ENABLE);/* Wait till PLL is ready */while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) = RESET)/* Select PLL as system clock source */RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);/* Wait till PLL is used as system clock source */while(RCC_GetSYSCLKSource() != 0x08)/* Enable peripheral clocks -*/* Enable DMA clock */RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);/* Enable ADC1 and GPIOC clock */RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);/* Enable USART1 and GPIOA clock */RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOA| RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);void SysTick_Configuration(void)/* Select AHB clock(HCLK) as SysTick clock source */SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);/* Set SysTick Priority to 3 */NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);/* SysTick interrupt each 1ms with HCLK equal to 72MHz */SysTick_SetReload(72000);/* Enable the SysTick Interrupt */SysTick_ITConfig(ENABLE);/* Function Name : GPIO_Configuration* Description : Configures the different GPIO ports.* Input : None* Output : None* Return : None*/void GPIO_Configuration(void)GPIO_InitTypeDef GPIO_InitStructure;/* Configure PC.03 (ADC Chann

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论