#include <p18cxxx.h>
#include <stdio.h>
#include <delays.h>
#include <spi.h>
#pragma config WDT = OFF, LVP = OFF, OSC = HS
/***********************************
Prototypes / Defines
***********************************/
void initSPI(void);
void e_togg(void);
void lcd_line(char line);
void lcd_cmd(unsigned char letter);
void lcd_char(unsigned char letter);
void lcd_string(char *senpoint);
void lcd_init(void);
void delay_ms(int ms);
void delay_us(int us);
void delay_s(int s);
void lcdByte(unsigned char dNyb,unsigned char rs);
char RTCRegRead(char adx);
void RTCRegWrite(char adx, char data);
#define CS LATCbits.LATC2
#define CS_T TRISCbits.TRISC2
#define SCL_T TRISCbits.TRISC3
#define SDA_T TRISCbits.TRISC4
#define SDO_T TRISCbits.TRISC5
#define LCD_DAT_T TRISCbits.TRISC0
#define LCD_CLK_T TRISCbits.TRISC1
#define LCD_DAT LATCbits.LATC0
#define LCD_CLK LATCbits.LATC1
#define LCD_E_T TRISCbits.TRISC6
#define LCD_E LATCbits.LATC6
char string[] = " ";
unsigned char time[16];
/***********************************
Main
***********************************/
void main(void){
unsigned char tmp,tmp2;
unsigned char buff[40];
char i;
ADCON1 = 0x0E;
initSPI();
lcd_init();
lcd_line(1);
sprintf(string," AtomSoftTech",0);
lcd_string(&string);
lcd_line(2);
sprintf(string," 2-Wire 74LS164",0);
lcd_string(&string);
delay_s(3);
lcd_line(2);
sprintf(string," DS1306 TEST. ",0);
lcd_string(&string);
delay_s(3);
RTCRegWrite(0x80,0x00);
RTCRegWrite(0x81,0x32);
RTCRegWrite(0x82,0x66);
//0x00
//0x23
//0x56
while(1){
tmp = RTCRegRead(0x02);
time[15] = (tmp >> 4) & 0x02;
if(time[15] == 0)
time[15] = 'A' ;
else
time[15] = 'P';
time[14] = 0x20;
tmp = RTCRegRead(0x00);
time[13] = (tmp & 0x0F) + 0x30;
time[12] = ((tmp >> 4) & 0x0F ) + 0x30;
time[11] = ':';
tmp = RTCRegRead(0x01);
time[10] = (tmp & 0x0F) + 0x30;
time[9] = ((tmp >> 4) & 0x0F ) + 0x30;
time[8] = ':';
tmp = RTCRegRead(0x02);
time[7] = (tmp & 0x0F) + 0x30;
time[6] = ((tmp >> 4) & 0x01 ) + 0x30;
time[5] = 0x20;
time[4] = ':';
time[3] = 'e';
time[2] = 'm';
time[1] = 'i';
time[0] = 'T';
lcd_line(2);
lcd_string(time);
delay_ms(100);
}
}
char RTCRegRead(char adx){
char tmp;
CS = 1;
WriteSPI(adx); //sec
tmp = ReadSPI();
CS = 0;
return tmp;
}
void RTCRegWrite(char adx, char data){
CS = 1;
WriteSPI(adx);
WriteSPI(data);
CS = 0;
}
void initSPI(void){
CS_T = 0; //CS is output
SCL_T = 0; //SCL is output
SDA_T = 1; //SDA is input
SDO_T = 0; //SDO is output
OpenSPI(SPI_FOSC_4,MODE_10,SMPEND); //[b]Had to change to SMPEND[/b]
CS = 1;
WriteSPI(0x8F);
WriteSPI(0x00);
CS = 0;
}
void lcd_string(char *senpoint){
delay_ms(1);
while(*senpoint != '\0'){
lcd_char(*senpoint);
senpoint++;
}
}
void lcdByte(unsigned char dNyb,unsigned char rs){
int i;
LCD_DAT=0; //Clear 74LS164 set initial bit to 0
for(i=0;i<8;i++){ //repeat for 8 bits
LCD_CLK=1;LCD_CLK=0; //write 0's to the 164
}
for(i=0;i<4;i++){ //output the nybble
if((dNyb & 0x08) != 0)
LCD_DAT=1;
else
LCD_DAT=0;
LCD_CLK=1;LCD_CLK=0;
dNyb=dNyb<<1;
}
LCD_DAT = rs; //output the RS bit value
LCD_CLK=1;LCD_CLK=0;
LCD_DAT = 0;
LCD_CLK=1;LCD_CLK=0;
LCD_CLK=1;LCD_CLK=0;
LCD_CLK=1;LCD_CLK=0;
e_togg();
}
void e_togg(void){
LCD_E=1;LCD_E=0;
}
void clk_togg(void){
LCD_CLK=1;LCD_CLK=0;
}
void lcd_line(char line){
if(line == 0x01)
lcd_cmd(0x80);
else
lcd_cmd(0xc0);
}
void lcd_cmd(unsigned char letter){
unsigned char temp;
temp=letter;
temp=temp>>4;
lcdByte(temp,0);
temp=letter;
temp=temp&0x0f;
lcdByte(temp,0);
}
void lcd_char(unsigned char letter){
unsigned char temp;
temp=letter;
temp=temp>>4;
lcdByte(temp,1);
temp=letter;
temp=temp&0x0f;
lcdByte(temp,1);
}
void lcd_init(void){
LCD_E_T = 0;
LCD_CLK_T = 0;
LCD_DAT_T = 0;
lcdByte(0x03,0);
delay_ms(5);
e_togg();
delay_us(160);
e_togg();
delay_us(160);
lcdByte(0x02,0);
delay_us(160);
lcd_cmd(0x28); //set 4-bit mode and 2 lines
delay_us(160);
lcd_cmd(0x10); //cursor move & shift left
delay_us(160);
lcd_cmd(0x06); //entry mode = increment
delay_us(160);
lcd_cmd(0x0d); //display on - cursor blink on
delay_us(160);
lcd_cmd(0x01); //clear display
delay_ms(500);
}
void delay_s(int s){
int y;
char x;
for(y=0;y<s;y++)
for(x=0;x<4;x++)
delay_ms(250);
}
void delay_ms(int ms){
int y;
for(y=0;y<ms;y++)
Delay1KTCYx(5);
}
void delay_us(int us){
int y;
char x;
for(y=0;y<us;y++)
for(x=0;x<5;x++)
Nop();
}