- //stm32内部AD采样程序。运行通过的请大家放心使用。
- //有缺陷的地方请大家多多指教。本人才刚刚起步,还有劳各位大侠带一把呀!
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x_lib.h"
- #include"stdio.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)//时钟sclk
- unsigned char Ver='A'; //驱动版本,默认为A版本,一共4个版本
- void delayms(unsigned int ii)//1ms延时函数
- {
- unsigned int i,x;
- for (x=0;x<ii;x++)
- {
- for (i=0;i<100;i++);
- }
- }
- void send(unsigned char value, unsigned char cd) //写8位数据
- {
- unsigned char i,dt;
- LED1OFF;
- LED4OFF;
- if(cd == 0)
- LED3OFF;
- else
- LED3ON;
- LED4ON;
- dt=value;
- for(i=0;i<8;i++)
- {
- LED4OFF;
- if(dt&0x80)
- LED3ON;
- else
- LED3OFF;
- dt=dt<<1;
- LED4ON;
- }
- LED1ON;//cs=1;
- }
- void readdata() //读数据
- {
- unsigned char i,j;
- unsigned char ch[6];
- send(0x09, 0); // 读数据
- LED1OFF;//cs=0;
- LED4OFF;//sclk=0;
- for (i=0;i<6;i++)
- {
- ch[i]=0;
- for (j=0;j<8;j++)
- {
- LED4ON;//sclk=1;
- if (!(GPIO_ReadOutputDataBit(GPIOF,GPIO_Pin_2)))//sdata==0)
- {
- ch[i]=ch[i]|(1<<(7-j));
- }
- LED4OFF;//sclk=0;
- }
- }
- switch(ch[0])
- {
- case 255:
- Ver='A';
- break;
- case 127:
- Ver='B';
- break;
- case 0:
- if(ch[1]+ch[2]+ch[3]==0)
- {
- Ver='D';
- }else
- {
- Ver='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); // NOP
- delayms(5);
- send(0x01, 0); // LCD Software Reset
- delayms(5);
- send(0xC6, 0); // Initial Escape
- send(0xB9, 0); // Refresh set
- send(0x00, 1);
- readdata(); //读出数据
- send(0xB5, 0); // Gamma Curve Set
- send(0x01, 1);
- send(0xbd, 0); //common driver output select//很重要的
- if(Ver=='D')
- {
- send(0x04, 1);
- }else
- {
- send(0x00, 1);
- }
- send(0xBE, 0); // Power Control
- send(0x03, 1);
- send(0x11, 0); // Sleep out
- send(0xBA, 0); // Power Control
- send(0x7F, 1);
- send(0x03, 1);
- send(0x36, 0); // 扫描方式
- if (Ver=='A')
- {
- send(0x00|0x08,1); //RGB
- }
- else
- {
- send(0x00,1); //RGB
- }
- send(0xB7, 0); // Temperature gradient set
- for(i=0; i<14; i++)
- {
- send(0, 1);
- }
- send(0x29, 0); //display ON
- send(0x03, 0); // Booster Voltage ON
- delayms(20); // Booster Voltage stabilisieren lassen
- send(0x20, 0); //display inversion OFF
- send(0x25, 0); // Write contrast
- switch(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 format
- send(0x03, 1); //0x03 为4096色,0x02为256色
- }
- else
- {
- send(0x3a, 0); //interface pixel format
- send(0x02, 1); //0x03 为4096色,0x02为256色
- send(0x2d, 0); //调色板设置
- if (Ver=='B'||Ver=='C')
- {
- //red
- send(~0x00, 1);
- send(~0x02, 1);
- send(~0x03, 1);
- send(~0x04, 1);
- send(~0x05, 1);
- send(~0x06, 1);
- send(~0x08, 1);
- send(~0x0f, 1);
- //green
- send(~0x00, 1);
- send(~0x02, 1);
- send(~0x03, 1);
- send(~0x04, 1);
- send(~0x05, 1);
- send(~0x06, 1);
- send(~0x08, 1);
- send(~0x0f, 1);
- //blue
- send(~0x00, 1);
- send(~0x03, 1);
- send(~0x06, 1);
- send(~0x0f, 1);
- }else
- {
- //red
- send(0x00, 1);
- send(0x02, 1);
- send(0x03, 1);
- send(0x04, 1);
- send(0x05, 1);
- send(0x06, 1);
- send(0x08, 1);
- send(0x0f, 1);
- //green
- send(0x00, 1);
- send(0x02, 1);
- send(0x03, 1);
- send(0x04, 1);
- send(0x05, 1);
- send(0x06, 1);
- send(0x08, 1);
- send(0x0f, 1);
- //blue
- send(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,结束Y
- {
- send(0x2a,0 );//column address set
- send(x1,1 );
- send(x2,1 );
- send(0x2B,0 );//page address set
- send(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 < 49;x ++)
- { send(value>>4,1);
- send(((value&0x0f)<<4)|(value>>8),1);
- send(value,1);
- }
- }
- }
- else
- {
- for(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 DEBUG
- debug();
- #endif
- /* System clocks configuration ---------------------------------------------*/
- RCC_Configuration();
- SysTick_Configuration();
- /* GPIO configuration ------------------------------------------------------*/
- GPIO_Configuration();
- //printf("\r\n USART1 print AD_value -------------------------- \r\n");
- /* 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 channel1
- DMA_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,结束Y
- while(1)
- {
- AD_value=ADC_GetConversionValue(ADC1);
- }
- /*{
- for(i=0;i<100;i++)
- {for(j=0;j<20;j++)
- {
- AD_value=ADC_GetConversionValue(ADC1);
- // Printf message with AD value to serial port every 1 second
- showdian(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 Channel13) as analog input -------------------------*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- // Configure PF. as Output push-pull
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOF, &GPIO_InitStructure);
- }
- #ifdef DEBUG
- /*******************************************************************************
- * Function Name : assert_failed
- * Description : Reports the name of the source file and the source line number
- * where the assert error has occurred.
- * Input : - file: pointer to the source file name
- * - line: assert error line source number
- * Output : None
- * Return : None
- *******************************************************************************/
- void assert_failed(u8* file, u32 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 2007 STMicroelectronics *****END OF FILE****/