📄 5x7matrix_main.c
字号:
///////////////////////////////////////////////////////////////////////////
/// ///
/// 5x7 Dot Matrix Display Single Color v01.01 ///
/// Software: Microchip c18 compiler ///
/// Microchip MPLAB IDE v8.00 ///
/// Hardware Details: ///
/// Controller : PIC18F4550 ///
/// Driver : ULN2803 : PORTA0 - PORTA4 ///
/// Latch : 74HC573 : PORTD0 - PORTD7 ///
/// Decoder Input : 74HC4514 : PORTB4 - PORTB7 ///
/// Decoder 1 Enable : PORTE1 ///
/// Decoder 2 Enable : PORTE2 ///
/// ///
///////////////////////////////////////////////////////////////////////////
#include "P18F4550.h"
#include "5x7_Config.c" //Configuration Fuses
#define EEPROM_ADDR 0xA0 //External EEPROM Memory Address - i2c Interface
#define EEPROM_WRITE 0xA0 //
#define EEPROM_READ 0xA1
#define RTC_ADDR 0xD0 //External RTC Address - i2c Interface
#define RTC_WRITE 0xD0
#define RTC_READ 0xD1
//Definitions for I/O Line Assignment
#define LATCH_DATA PORTD
#define DRIVER PORTA
#define DECODER_INPUT PORTB
#define DECODER_2E PORTEbits.RE1
#define DECODER_1E PORTEbits.RE2
//Definitions for Column Selection or Driver Input
#define NONE 0
#define FIRST_COLUMN 1
#define SECOND_COLUMN 2
#define THIRD_COLUMN 4
#define FOUTH_COLUMN 8
#define FIFTH_COLUMN 16
//Internal EEPROM ADDRESS Assignement for initialisation
//While Start up the values for every control register will read the data from
//the corresponding EEPROM Memory location.
#define Total_Seg_Addr 0x00 //Total number of segments in the display
#define Run_Speed_Addr 0x01 //Display running speed value location
#define Total_BytesL 0x02 //Total no bytes to display
#define Total_BytesH 0x03 //
#define Total_BytesU 0x04
#define Start_addressL 0x05 //Starting address location of External EEPROM memory
#define Start_addressH 0x06
#define Start_addressU 0x07
#define Disp_Direction 0x08 //Direction Indication register : 0x10 - Right-To-Left ::: 0x20 - Left-To-Right ::: 0x30 - Fixed
#define Timee_Display 0x09
#pragma udata EEPROM_Buffer = 0x100
ram unsigned char Buffer[250];
#pragma udata
ram unsigned char RTC_Buffer[100];
//#pragma udata Temp_Buffer = 0x200
//ram unsigned char buf[128];
//#pragma udata
ram unsigned char Total_Segment;
ram unsigned char Run_Speed;
ram unsigned char Display_Direction;
ram unsigned short long int Disp_Start_Address;
ram unsigned short long int Total_Bytes;
ram unsigned short long int address;
ram unsigned char eeprom_read_command,eeprom_write_command;
far ram unsigned char temp,temph,treg;
far rom unsigned char *romptr;
ram unsigned char Time,tcount = 0,sec,min,hour;
void io_init(void);
void Fill_Buffer(unsigned short long int *addr,unsigned char bufptr);
void Load_Latch(unsigned char);
void EEPROM_Write(unsigned char,unsigned char);
unsigned char EEPROM_Read(unsigned char);
void Ex_EEPROM_Block_Select(unsigned char);
void Fill_PrePost_Zero(unsigned char al,unsigned char ah);
void New_Message(void);
void usart_interrupt_routine(void);
void Time_Display(void);
unsigned char Calculate_hour(void);
unsigned char Calculate_min(unsigned char i);
void Calculate_sec(unsigned char i);
void loadbuffer(void);
#include "Luk_up.c" // Luk up for Font
#include "delay.c" // Dealy Routine
#include "usart.c" // Usart Funtions
#include "i2c.c" // i2c Functions
#include "Exter_EEPROM.c"
#include "Inter_EEPROM.c"
//Interrupt Vector Routine
void Interrupt_high(void);
#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh(void)
{
_asm
goto Interrupt_high
_endasm
}
#pragma code
#pragma interrupt Interrupt_high
void Interrupt_high()
{
if(PIR1bits.RCIF & PIE1bits.RCIE & IPR1bits.RCIP)
{
usart_interrupt_routine();
}
}
void main()
{
ram unsigned char i,j,k,low,high,upper;
ram unsigned int temp_run_speed,ii;
ram unsigned short long int temp_total_bytes,temp_address;
io_init();
usart_init();
i2c_init();
PORTEbits.RE0 = 1;
PIR1bits.RCIF = 0;
IPR1bits.RCIP = 1;
PIE1bits.RCIE = 1;
INTCONbits.PEIE = 1;
INTCONbits.GIE = 1;
Total_Segment = EEPROM_Read(Total_Seg_Addr); //Load Total Number of Segment
low = EEPROM_Read(Total_BytesL); //Load Total Number of Bytes to Display
high = EEPROM_Read(Total_BytesH);
upper = EEPROM_Read(Total_BytesU);
Total_Bytes = (((unsigned short long int)upper<<16) | ((unsigned short long int)high<<8) | ((unsigned short long int)low));
low = EEPROM_Read(Start_addressL); //Load the Starting ADDRESS of EEPROM to fetch the data
high = EEPROM_Read(Start_addressH);
upper = EEPROM_Read(Start_addressU);
Disp_Start_Address = (((unsigned short long int)upper<<16) | ((unsigned int)high<<8) | ((unsigned int)low));
Display_Direction = EEPROM_Read(Disp_Direction); // Data to Denote Display Direction
Time = EEPROM_Read(Timee_Display); // Data to represnt to display time or not
while(1)
{
temp_total_bytes = 0;
address = Disp_Start_Address;
while((temp_total_bytes <= Total_Bytes) && (Total_Bytes != 0))
{
Run_Speed = EEPROM_Read(Run_Speed_Addr);
temp_run_speed = 0;
treg = 0;
while(temp_run_speed < Run_Speed)
{
temp_address = address;
if(treg == 0)
Fill_Buffer(&temp_address,0);
DRIVER = NONE;
Load_Latch(0);
DRIVER = FIRST_COLUMN;
temp_address++;
if(treg == 0)
Fill_Buffer(&temp_address,33);
DRIVER = NONE;
Load_Latch(33);
DRIVER = SECOND_COLUMN;
temp_address++;
if(treg == 0)
Fill_Buffer(&temp_address,65);
DRIVER = NONE;
Load_Latch(65);
DRIVER = THIRD_COLUMN;
temp_address++;
if(treg == 0)
Fill_Buffer(&temp_address,97);
DRIVER = NONE;
Load_Latch(97);
DRIVER = FOUTH_COLUMN;
temp_address++;
if(treg == 0)
Fill_Buffer(&temp_address,129);
DRIVER = NONE;
Load_Latch(129);
DRIVER = FIFTH_COLUMN;
temp_run_speed++;
// treg++;
if(Time)
Time_Display();
}
delay_ms(Run_Speed);
if(Display_Direction == 0x10)
address++;
else if(Display_Direction == 0x20)
address--;
temp_total_bytes++;
}
}
}
//USART Interupt Service Routine
void usart_interrupt_routine()
{
temp = getc_ram();
if(temp == 'R')
{
putc_ram(~temp);
temp = getc_ram();
if(temp == 'r')
{
temp = getc_ram();
if(temp == 'M') //New Message
{
New_Message();
Reset();
}
else if(temp == 'R') //Running Speed
{
temp = getc_ram();
EEPROM_Write(Run_Speed_Addr,temp);
putc_ram('A');
}
else if (temp == 'D') //Display Starting Direction
{
temp = getc_ram();
if(Total_Bytes != 0)
{
EEPROM_Write(Disp_Direction,temp);
if(temp == 0x10) //Running from Left To Right
{
EEPROM_Write(Start_addressL,0);
EEPROM_Write(Start_addressH,0);
EEPROM_Write(Start_addressU,0);
}
else if(temp == 0x20) //Running from Right To Left
{
EEPROM_Write(Start_addressL,Total_Bytes);
EEPROM_Write(Start_addressH,(Total_Bytes>>8));
EEPROM_Write(Start_addressU,(Total_Bytes>>16));
}
else if(temp == 0x30) //No Running
{
EEPROM_Write(Start_addressL,(Total_Segment*5));
EEPROM_Write(Start_addressH,0);
EEPROM_Write(Start_addressU,0);
}
putc_ram('A');
while(!TXSTAbits.TRMT);
Reset();
}
else
putc_ram('E');
}
else if (temp == 'S') //Total Number of Segment
{
temp = getc_ram();
EEPROM_Write(Total_Seg_Addr,temp);
putc_ram('A');
while(!TXSTAbits.TRMT);
Reset();
}
else if(temp == 'T') // Time
{
temp = getc_ram();
if(temp == 'S') //Set Time
{
sec = getc_ram();
min = getc_ram();
hour = getc_ram();
temp = getc_ram();
if(temp == 0x12)
hour = hour | 0x40;
else if (temp == 0x24)
hour = hour & 0x3F;
i2c_start();
i2c_write(RTC_WRITE);
i2c_write(0);
i2c_write(sec);
i2c_write(min);
i2c_write(hour);
i2c_stop();
putc_ram('A');
}
else if(temp == 'D') //Time Display Type
{
temp = getc_ram();
EEPROM_Write(Timee_Display,temp);
putc_ram('A');
}
while(!TXSTAbits.TRMT);
Reset();
}
else
putc_ram('E');
}
else
putc_ram('E');
}
else
putc_ram('E');
}
//Input and Output PORT Initialisation
void io_init()
{
TRISA = 0; // All lines are output
TRISB = 0; // All lines are output
TRISC = 0; // All lines are output
TRISD = 0; // All lines are output
TRISE = 0; // All lines are output
LATA = 0; // Clear the Latch
LATB = 0; // Clear the Latch
LATC = 0; // Clear the Latch
LATD = 0; // Clear the Latch
LATE = 0; // Clear the Latch
PORTA = 0; // Clear the PORT
PORTB = 0; // Clear the PORT
PORTC = 0; // Clear the PORT
PORTD = 0; // Clear the PORT
PORTE = 0; // Clear the PORT
ADCON1 = 0x0F; //Disable All Analog Ports and Configure as Digital
SPPCON = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -