📄 main.c
字号:
};
unsigned short int ADC_ConvertedValue;
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
ErrorStatus HSEStartUpStatus;
extern vu32 TimingDelay;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void Delay(vu32 nTime);
void SysTick_Configuration(void);
void SetupLED (void) ;
extern void SetupADC (void);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : SysTick_Configuration
* Description : Configures the SysTick to generate an interrupt each 1 millisecond.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
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 : 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);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* PLLCLK = 8MHz * 9 = 72 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 GPIOB, GPIOC and AFIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
RCC_APB2Periph_AFIO, ENABLE);
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable the EXTI9_5 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//---------------------------------------
//名称: 使用SPI接口写数据到LCD
//参数:dt:写入的数据 command: 1-数据/0-命令
//作者:cby
//网站:宁波单片机开发网
//网址:www.nbdpj.com
//邮箱:nbdpj@nbdpj.com
//日期:20081111
//-----------------------------------------
void LCD_write_byte(unsigned char dt, unsigned char command)
{
unsigned char i;
sce0;
if(command)
dc1;
else
dc0;
for(i=0;i<8;i++)
{
if(dt&0x80)
sdin1;
else
sdin0;
dt=dt<<1;
sclk0;
sclk1;
sclk0;
}
}
//---------------------------------------
//名称: 5110LCD初始化函数
//作者:cby
//网站:宁波单片机开发网
//网址:www.nbdpj.com
//邮箱:nbdpj@nbdpj.com
//日期:20081111
//-----------------------------------------
void LCD_init(void)
{
sce1;
res1;
Delay(10);
res0;
Delay(100);
res1;
Delay(10);
LCD_write_byte(0x21,0);//LCD功能设置:芯片活动,水平寻址,使用扩展指令
LCD_write_byte(0xC0,0);//设置VOP值,室温下的编程范围为3.00-10.68
//Vlcd=3.06+(VOP)*0.06,本例VOP为0B0101 0000为十进制的80,Vlcd=7.86V
LCD_write_byte(0x20,0);//LCD功能设置:芯片活动,水平寻址,使用基本指令
LCD_write_byte(0x0C,0);//设定显示配置:普通模式
sce1;
}
//---------------------------------------
//名称: 设置坐标函数
//参数:X:0-83 Y:0-5
//作者:cby
//网站:宁波单片机开发网
//网址:www.nbdpj.com
//邮箱:nbdpj@nbdpj.com
//日期:20081111
//-----------------------------------------
void LCD_set_XY(unsigned char X, unsigned char Y)
{
LCD_write_byte(0x40 | Y, 0);// column
LCD_write_byte(0x80 | X, 0);// row
sce1;
}
//---------------------------------------
//名称: LCD清屏函数
//作者:cby
//网站:宁波单片机开发网
//网址:www.nbdpj.com
//邮箱:nbdpj@nbdpj.com
//日期:20081111
//-----------------------------------------
void LCD_clear(void)
{
unsigned char t;
unsigned char k;
LCD_set_XY(0,0);
for(t=0;t<6;t++)
{
for(k=0;k<84;k++)
{
LCD_write_byte(0x00,1);
}
}
sce1;
}
//---------------------------------------
//名称: 显示英文字符
//参数:c:显示的字符在font6x8表格中的位置
//作者:cby
//网站:宁波单片机开发网
//网址:www.nbdpj.com
//邮箱:nbdpj@nbdpj.com
//日期:20081111
//-----------------------------------------
void LCD_write_char(unsigned char c)
{
unsigned char line;
c-= 32;
for (line=0; line<6; line++)
LCD_write_byte(font6x8[c][line], 1);
}
//---------------------------------------
//名称: 英文字符串显示函数
//参数:*s:英文字符串指针
//作者:cby
//网站:宁波单片机开发网
//网址:www.nbdpj.com
//邮箱:nbdpj@nbdpj.com
//日期:20081111
//-----------------------------------------
void LCD_write_String(unsigned char X,unsigned char Y,char *s)
{
LCD_set_XY(X,Y);
while (*s)
{
LCD_write_char(*s);
s++;
}
}
//---------------------------------------
//名称: 写一个字符到LCD函数(8*16点阵)
//参数:row,page:写入字符的地址 c: 写入字符在shuzi表格中的位置
//作者:cby
//网站:宁波单片机开发网
//网址:www.nbdpj.com
//邮箱:nbdpj@nbdpj.com
//日期:20081111
void LCD_write_shu(unsigned char row, unsigned char page,unsigned char c) //row:列 page:页 dd:字符
{
unsigned char i;
LCD_set_XY(row*8, page);// 列,页
for(i=0; i<8;i++)
{
LCD_write_byte(shuzi[c*16+i],1);
}
LCD_set_XY(row*8, page+1);// 列,页
for(i=8; i<16;i++)
{
LCD_write_byte(shuzi[c*16+i],1);
}
sce1;
}
//---------------------------------------
//名称: 写一个汉字到LCD函数
//参数:x,y:写入汉字的地址 address: 写入汉字在han表格中的位置
//作者:cby
//网站:宁波单片机开发网
//网址:www.nbdpj.com
//邮箱:nbdpj@nbdpj.com
//日期:20081111
//-----------------------------------------
void LCD_write_hanzi(unsigned char row, unsigned char page,unsigned char c) //row:列 page:页 dd:字符
{
unsigned char i;
LCD_set_XY(row*8, page);// 列,页
for(i=0; i<16;i++)
{
LCD_write_byte(han[c*32+i],1);
}
LCD_set_XY(row*8, page+1);// 列,页
for(i=16; i<32;i++)
{
LCD_write_byte(han[c*32+i],1);
}
sce1;
}
//---------------------------------------
//名称: IO初始化函数
//作者:cby
//网站:宁波单片机开发网
//网址:www.nbdpj.com
//邮箱:nbdpj@nbdpj.com
//日期:20081111
//-----------------------------------------
void GPIO_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //激活GPIOA clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //激活GPIOB clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //激活GPIOC clock
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_12; //设置GPIOB0、GPIOB1、GPIOB12
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //设置GPIO速度为10MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //设置以上三个GPIO为输出
GPIO_Init(GPIOB, &GPIO_InitStructure); //将以上设置参数写入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5; //设置GPIOC4、GPIOC5
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //设置GPIO速度为10MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //设置以上二个GPIO为输出
GPIO_Init(GPIOC, &GPIO_InitStructure); //将以上设置参数写入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //设置GPIOA8
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //设置GPIO速度为10MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //设置以上GPIO为输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //将以上设置参数写入
}
//---------------------------------------
//名称: 主函数
//作者:cby
//网站:宁波单片机开发网
//网址:www.nbdpj.com
//邮箱:nbdpj@nbdpj.com
//日期:20081111
//-----------------------------------------
int main(void)
{
RCC_Configuration(); //设置系统时钟
SysTick_Configuration();
GPIO_Configuration(); //初始化
LCD_init(); //初始化LCD模块
LCD_clear(); //清屏幕
LCD_write_hanzi(0,0,0); //宁
LCD_write_hanzi(2,0,1); //波
LCD_write_hanzi(4,0,2); //单
LCD_write_hanzi(6,0,3); //片
LCD_write_hanzi(8,0,4); //机
LCD_write_hanzi(1,2,5); //开
LCD_write_hanzi(4,2,6); //发
LCD_write_hanzi(7,2,7); //网
LCD_write_String(1,5,"www.nbdpj.com"); //www.nbdpj.com
backled0; //开背光
while(1)
{
}
}
/*******************************************************************************
* Function Name : Delay
* Description : Inserts a delay time.
* Input : nTime: specifies the delay time length, in milliseconds.
* Output : None
* Return : None
*******************************************************************************/
void Delay(u32 nTime)
{
/* Enable the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Enable);
TimingDelay = nTime;
while(TimingDelay != 0);
/* Disable the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Disable);
/* Clear the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Clear);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -