c51 实验板 编写8个按键控制程序,按下8个键,在数码管上分别显示1-8

c51 实验板 编写8个按键控制程序,按下8个键,在数码管上分别显示1-8,第1张

其实方法很多的,这只是简单的一种

#include<reg52.h>

#define uchar unsigned char

#define uint unsigned int

#define KEYDOWN 1//按键按下

#define KEYUP 0//按键d开

#define led_date P0 //LED数据口

bit key_first = 1 //按键第一次按下

bit key_state = KEYUP //按键状态

uchar key_value//键值

code uchar LED[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,}

//LED段码 共阳管

void main(void)

void key_scan(void)

void key_scan()

{

uchar key_buff,key_date

key_date = P1 &0xFF

if(key_date != 0xFF)

{

if(key_state != KEYDOWN)

{

if(key_first == 1)

{

key_buff = key_date

key_first = 0

}

else

{

if(key_date == key_buff)

{

key_value = ~key_date

key_state = KEYDOWN

}

}

}

}

else

{

key_first = 1

key_state = KEYUP

}

}

void main()

{

while(1)

{

key_scan()

switch(key_value)

{

case 0x01:

{

led_date = LED[1]

}break

case 0x02:

{

led_date = LED[2]

}break

case 0x04:

{

led_date = LED[3]

}break

case 0x08:

{

led_date = LED[4]

}break

case 0x10:

{

led_date = LED[5]

}break

case 0x20:

{

led_date = LED[6]

}break

case 0x40:

{

led_date = LED[7]

}break

case 0x80:

{

led_date = LED[8]

}break

default: break

}

}

}

下面这个代码会短一些

#include<reg52.h>

#define uchar unsigned char

#define uint unsigned int

#define KEYDOWN 1//按键按下

#define KEYUP 0//按键d开

#define led_date P0 //LED数据口

bit key_first = 1 //按键第一次按下

bit key_state = KEYUP //按键状态

uchar key_value//键值

code uchar LED[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,}

//LED段码 共阳管

void main(void)

void key_scan(void)

void key_scan()

{

uchar i,key_buff,key_date

key_date = P1 &0xFF

if(key_date != 0xFF)

{

if(key_state != KEYDOWN)

{

if(key_first == 1)

{

key_buff = key_date

key_first = 0

}

else

{

if(key_date == key_buff)

{

for(i=0i<8i++)

{

if(!((key_date>>i)&0x01))

{

break

}

}

key_value = LED[i+1]

key_state = KEYDOWN

}

}

}

}

else

{

key_first = 1

key_state = KEYUP

}

}

void main()

{

while(1)

{

key_scan()

led_date = key_value

}

}

在mian函数里面写程序,具体主函数如下:

#include "stm32f10x_lib.h"

int m=0

int n=0

#include "lcd.h"

char b[10]={'0','1','2','3','4','5','6','7','8','9'}

typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus

vu32 ret

volatile TestStatus TestRx

void RCC_Configuration(void)

void GPIO_Configuration(void)

void NVIC_Configuration(void)

void CAN_Config(void)

TestStatus CAN_Polling(void)

TestStatus CAN_Interrupt(void)

void SysTick_Config(void)

void LcdShow_Init(void)

void Led_Config(void)

void Button_Config(void)

void Led_RW_ON(void)

void Led_RW_OFF(void)

void delay(void)

void delay()

{

int i,j

j=0

for (i=0i<0xfffffi++) j++

}

EXTI_InitTypeDef EXTI_InitStructure

int main(void)

{

char a[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',

'Q','R','S','T','U','V','W','X','Y','Z'}

#ifdef DEBUG

debug()

#endif

RCC_Configuration()

GPIO_Configuration()

NVIC_Configuration()

SysTick_Config()

LcdShow_Init()

Led_Config()

Button_Config()

GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource3)

EXTI_InitStructure.EXTI_Line = EXTI_Line3

EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt

EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling

EXTI_InitStructure.EXTI_LineCmd = ENABLE

EXTI_Init(&EXTI_InitStructure)

GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource4)

EXTI_InitStructure.EXTI_Line = EXTI_Line4

EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt

EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling

EXTI_InitStructure.EXTI_LineCmd = ENABLE

EXTI_Init(&EXTI_InitStructure)

while(1)

{

write_char(a+m,0,0)

n=a[m]

delay()

delay()

if(m==26)

{m=0}

}

}

void RCC_Configuration(void)

{

ErrorStatus HSEStartUpStatus

RCC_HSEConfig(RCC_HSE_ON)

HSEStartUpStatus = RCC_WaitForHSEStartUp()

if(HSEStartUpStatus == SUCCESS)

{

RCC_HCLKConfig(RCC_SYSCLK_Div1)

RCC_PCLK2Config(RCC_HCLK_Div1)

RCC_PCLK1Config(RCC_HCLK_Div2)

RCC_ADCCLKConfig(RCC_PCLK2_Div6)

FLASH_SetLatency(FLASH_Latency_2)

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable)

RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9)

RCC_PLLCmd(ENABLE)

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)

{

}

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK)

while(RCC_GetSYSCLKSource() != 0x08)

{

}

}

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC

| RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE)

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE)

RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE)

}

void GPIO_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 |GPIO_Pin_6 | GPIO_Pin_7

| GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz

GPIO_Init(GPIOC, &GPIO_InitStructure)

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_11 | GPIO_Pin_12 |

GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING

GPIO_Init(GPIOD, &GPIO_InitStructure)

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU

GPIO_Init(GPIOD, &GPIO_InitStructure)

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP

GPIO_Init(GPIOD, &GPIO_InitStructure)

GPIO_PinRemapConfig(GPIO_Remap2_CAN,ENABLE)

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz

GPIO_Init(GPIOE, &GPIO_InitStructure)

}

void NVIC_Configuration(void)

{

NVIC_InitTypeDef NVIC_InitStructure

#ifdef VECT_TAB_RAM

NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0)

#else

NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0)

#endif

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2)

NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQChannel

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE

NVIC_Init(&NVIC_InitStructure)

NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQChannel

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE

NVIC_Init(&NVIC_InitStructure)

NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE

NVIC_Init(&NVIC_InitStructure)

NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 2, 0)

}

void LcdShow_Init(void)

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure

TIM_OCInitTypeDef TIM_OCInitStructure

TIM_TimeBaseStructure.TIM_Period = 8000

TIM_TimeBaseStructure.TIM_Prescaler = 17

TIM_TimeBaseStructure.TIM_ClockDivision = 0x0

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure)

TIM_ARRPreloadConfig(TIM2,DISABLE)

TIM_UpdateRequestConfig(TIM2,TIM_UpdateSource_Regular)

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing

TIM_OCInitStructure.TIM_Channel = TIM_Channel_1

TIM_OCInitStructure.TIM_Pulse = 4000

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High

TIM_OCInit(TIM2, &TIM_OCInitStructure)

TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable)

TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_Update, ENABLE)

TIM_Cmd(TIM2, ENABLE)

}

void SysTick_Config(void)

{

SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK)

SysTick_SetReload(720000)

SysTick_ITConfig(ENABLE)

SysTick_CounterCmd(SysTick_Counter_Enable)

}

void Button_Config(void)

{

GPIO_InitTypeDef GPIO_InitStructure

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE)

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz

GPIO_Init(GPIOD, &GPIO_InitStructure)

}

void Led_Config(void)

{

GPIO_InitTypeDef GPIO_InitStructure

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE)

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_4 | GPIO_Pin_5

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz

GPIO_Init(GPIOC, &GPIO_InitStructure)

}

void Led_RW_ON(void)

{

m++

}

void Led_RW_OFF(void)

{

int i,j

i=n/10

j=n%10

write_char(b+i,0,2)

write_char(b+j,0,3)

}

#ifdef DEBUG

void assert_failed(u8* file, u32 line)

{

while (1)

{

}

}

#endif

需要准备一个U盘就可以更换所有的LVDS接口的液晶屏程序。

U盘升级方法:

把屏所对应的程序复制到U盘中

把U盘插到板上的USB口上

接上按键板,按住按键板上的任意一个按键再通电,直到看到按键板上的指示灯出现红绿交替闪烁后,再松开按键,直到指示灯闪烁结束后,拔掉电源,再拔掉U盘就OK了,重新通电即可. 看你选择用哪种方式刷程序,可以用USB接口,25针并口,U盘等,像你说的V29我们通常用U盘写(如果板上没USB接口就用自制的转接口(一端插U盘一端连接在板上,也就是4针的通常为兰色的接口。不要说找不到哦,板上4针的接口不多且还标明了的)写)按照你这屏的的型号先拷个M190A1程序到U盘,先插上U盘,再给板供电,当显示灯红绿交替闪烁时就说明正在写程序,当显示灯闪烁的频率加快了时就已经写好了,此时先断电,再拔U盘,之后就可以全部连上看图像对不对了(要连信号线,看有无干扰,非灯管原因引起的图像抖动,分辨率对不对等)。

至此,在写完程序后全部连接上时是要将驱动板、驱屏线、升压板、灯管线及按键上的显示灯(按键可以不要,但显示的状态灯一定要要)连接好(不要把屏线插错了),将板上电压跳到5V,要跳5V哦。

注意,刷好程序连接了全套的东西时在通电瞬间准备好拔电,防止因屏内的短路或因插屏线没插好引起的短路(甚至拿屏线时不小心的粗心大意拿成了反双八的屏线了)乐华板的好处就在这里,特别是PC板在通电很短的短时间内如果显示灯颜色不正常立即断电是绝大多数情况下可以避免烧板或烧屏的


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8016658.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存