本项目旨在做通用显示器件,直接利用串口发送数据即可显示,方便以后复用。显示屏RAV4拆机VFD,SPI驱动。MCUAtmega328p,使用其他mcu需要修改寄存器。串口波特率,115200,8n1。具体指令见注释。编译环境atmel studio 6.2
/************************************************************************
建立时间:2015.08.25
功能:串口输入指令和数据并在屏幕上显示。
指令: AT+L0=**********;第一行显示内容**********
AT+L1=**********;第二行显示内容**********
AT+L2=**********;第三行显示内容**********
AT+L3=**********;第四行显示内容**********
AT+PW=0;灯丝、高压关。
AT+PW=1;灯丝、高压开。
AT+BR=*;*:0-9设置亮度。0最低,9最高。
AT+RE=1;VFD复位,重新初始化。
屏幕:RAV4车载cd拆机VFD
MCU:ATMEGA328P CRYSTAL :16MHZ
硬件连接: en(灯丝电源、高压使能)》》PB1
cs(SPI片选使能)》》PB2(SS)
data(串行数据输入)》》PB3(MOSI)
clock(SPI时钟)》》PB5(SCK)
修改时间:
修改内容:
************************************************************************/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "zimo.h"
/************************************************************************/
/* 常量定义 */
/************************************************************************/
#define cs PORTB2
#define en PORTB1
#define en_1() PORTB|=(1<<en)
#define en_0() PORTB&=~(1<<en)
#define cs_1() PORTB|=(1<<cs)
#define cs_0() PORTB&=~(1<<cs)
/************************************************************************/
/* 变量定义 */
/************************************************************************/
#define Rx_Buffer_len 37 //显示数据区31+AT命令区6
unsigned int Rx_Buffer_Position;
unsigned char Rx_Buffer[Rx_Buffer_len];
unsigned char Rx_End_flag;
unsigned char AT_cmd;
/************************************************************************/
/* 初始化IO */
/************************************************************************/
void GPIO_Init(void)
{
DDRB|=(1<<cs)|(1<<en);
}
/************************************************************************/
/* 串口初始化函数,115200 8 n 1,开串口接收中断使能 */
/************************************************************************/
void USART_Init()
{
cli();//关总中断
/* Set baud rate 115200*/
UBRR0H = (unsigned char)0;
UBRR0L = (unsigned char)0x08;
/* Enable receiver and transmitter and RX Complete Interrupt Enable*/
UCSR0B = (1<<RXCIE0)|(1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 1stop bit */
UCSR0C =(1<<UCSZ01)|(1<<UCSZ00);
sei();//开总中断
DDRD|=(1<<PORTD1);
}
/************************************************************************/
/* 串口数据发送,输入变量为字符 */
/************************************************************************/
void USART_Send( char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A&(1<<UDRE0)) );
/* Put data into buffer, sends the data */
UDR0 = data;
}
/************************************************************************/
/* 串口发送字符串 */
/************************************************************************/
void USART_Send_Str(char *s)
{
while(*s)
{
USART_Send(*s);
s++;
}
}
/************************************************************************/
/* 清串口接受缓存数组 */
/************************************************************************/
void Clear_Rx_Buffer()
{
unsigned int i;
for (i=0;i<Rx_Buffer_len;i++)
{
Rx_Buffer[i]=0x00;
}
Rx_Buffer_Position=0;
Rx_End_flag=0;
AT_cmd=0;
}
/************************************************************************/
/* 判断串口缓存区是否是AT指令 */
/************************************************************************/
void Check_Rx_buffer()
{
if (Rx_Buffer[0]=='A'&&Rx_Buffer[1]=='T'&&Rx_Buffer[2]=='+'&&Rx_Buffer[5]=='=')
{
AT_cmd=1;
}
else
{
USART_Send_Str("AT False!");
Clear_Rx_Buffer();
}
}
/************************************************************************/
/* 串口中断程序讲收到的数据存入缓存数组,若数组满,从头开始 */
/************************************************************************/
ISR(USART_RX_vect)
{
unsigned char rx_temp;
rx_temp=UDR0;
Rx_Buffer[Rx_Buffer_Position]=rx_temp;
Rx_Buffer_Position++;
if (Rx_Buffer_Position==Rx_Buffer_len)
{
Rx_Buffer_Position=0;
}
if (rx_temp==0x0d)
{
Rx_End_flag=0xff;
Check_Rx_buffer();
}
}
/************************************************************************/
/* 功能:返回输入字符的索引地址0-61;若无,返回62 */
/************************************************************************/
int Get_Index(unsigned char in_arr)
{
unsigned char index;
index=in_arr-0x20;
if (index>94)
{
index=0;
}
return index;
}
/************************************************************************/
/* 初始化SPI接口 LSB&#x