#include <mc9s12c32.h>
#include <datatypes.h>
#include <chips12_lcd.h>
#include <ofunc.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <afunc.h>
UINT8 data, adress;
void putchar_spi(int cx){
int temp;
while(!(SPISR & 0b00100000)); /* wait until write is permissible */
SPIDR = cx; /* output the byte to the SPI */
while(!(SPISR & 0b10100000)); /* wait until write operation is complete */
temp=SPIDR; // clear the spif flag.
}
void RTC_on(){
PORTA = 0x00; // CS pin is driven LOW
}
void RTC_off(){
PORTA = 0x01; // CS pin is driven HIGH
}
void init_SPI(){
SPICR1 = 0x50;
SPICR2 = 0x00;
DDRM |= 0x38; //set SS,SCK,MOSI lines of the µC to Output
SPIBR=0x06; //Bus clock:8MHz
}
void main(void){
//Configuration of the chip select pin
DDRA = 0x01; // Port A, Pin 0 controls Shutdown of DS1391
//Configuration & initialization of the SPI module
init_SPI();
//Switch on the RTC
RTC_on();
//Write the adress byte to the RTC
adress=0x82;
data=0x15;
putchar_spi(adress);
putchar_spi(data);
//Switch off the RTC
RTC_off();
ledon(1);
delay_ms(1000);
ledoff(1);
while (1){
RTC_off();
putchar_spi(adress);
putchar_spi(data);
RTC_off();
}
}