📄 m8demo.c
字号:
//ICC-AVR application builder : 2003-11-10 10:04:46
// Target : M8
// Crystal: 7.3728Mhz
#include <iom8.h>
#include <macros.h>
#include "M8Demo.h"
#include"I2C.H"
#include "Eeprom.h"
#include "AT45DB021.h"
#include "Uart.h"
#include "assembly.h"
unsigned int DelayTimer;
unsigned int TimerCount;
unsigned int TimerCountOld;
unsigned int key_tick;
unsigned char key_state;
unsigned char key_value;
unsigned char key_pressed;
unsigned char mode_state;
unsigned char *OutPutStr;
unsigned char out_eeprom_data;
unsigned char out_eeprom_addr=0;
unsigned char in_eeprom_data;
unsigned char in_eeprom_addr=0;
unsigned int adc_value=0;
unsigned char AT45DB021_data;
unsigned char AT45DB021_addr=0;
unsigned char Display_Position=0;
const char DataTab[16]=
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,
0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8E};
void Delay(unsigned int delay_time)
{
while(delay_time)
delay_time--;
}
void Delayms(unsigned int timers)
{
DelayTimer=0;
while(1)
{if(DelayTimer>timers)
return;
}
}
void port_init(void)
{
PORTB = 0xFF;
DDRB = 0xFF;
PORTC = 0x7F; //m103 output only
DDRC = 0x79;
PORTD = 0xFF;
DDRD = 0xFC;
}
//TIMER0 initialisation - prescale:1024
// WGM: Normal
// desired value: 10mSec
// actual value: 10.000mSec (0.0%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x8D; //set count
TCCR0 = 0x03; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 0x8D; //reload counter value
// TCNT0 = 0xA4; //reload counter value
DelayTimer++;
key_tick++;
Display_Position++;
if(Display_Position>=5)
Display_Position=1;
switch(mode_state)
{
case clock_mode: DisplayTimer(TimerCount);break;
case out_eeprom_addr_modify_mode:
case out_eeprom_data_modify_mode:
case out_eeprom_read_mode:DisplayHexData(out_eeprom_addr,out_eeprom_data);break;
case in_eeprom_addr_modify_mode:
case in_eeprom_data_modify_mode:
case in_eeprom_read_mode:DisplayHexData(in_eeprom_addr,in_eeprom_data);break;
case adc_mode:DisplayData( adc_value);break;
case AT45DB021_addr_modify_mode:
case AT45DB021_data_modify_mode:DisplayHexData(AT45DB021_addr,AT45DB021_data);break;
default :break;
}
}
//TIMER1 initialisation - prescale:256
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1Sec
// actual value: 1.000Sec (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x8F; //setup
TCNT1L = 0x80;
OCR1AH = 0x70;
OCR1AL = 0x80;
OCR1BH = 0x70;
OCR1BL = 0x80;
ICR1H = 0x70;
ICR1L = 0x80;
TCCR1A = 0x00;
TCCR1B = 0x04; //start Timer
}
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H = 0x8F; //reload counter high value
TCNT1L = 0x80; //reload counter low value
TimerCount++;
if(TimerCount==3600)
TimerCount=0;
}
void spi_init(void)
{
SPCR = 0x70; //setup SPI
SPSR = 0x00; //setup SPI
}
//TWI initialisation
// bit rate:1
void twi_init(void)
{
TWCR= 0X00; //disable twi
TWBR= 0x01; //set bit rate
TWSR= 0x00; //set prescale
TWAR= 0x02; //set slave address
TWCR= 0x44; //enable twi
}
void twi_Enable(void)
{
TWCR= 0x44; //enable twi
}
void twi_Disable(void)
{
TWCR= 0x00; //enable twi
}
//ADC initialisation
// Conversion time: 208uS
void adc_init(void)
{
ADCSR = 0x00; //disable adc
ADMUX = 0x00; //select adc input 0
ACSR = 0x00;
ADCSR = 0xc8;
}
void adc_Enable(void)
{
DDRC&=0xFE;
PORTC&=0xFE;
ADCSR = 0xC8;
}
void adc_Disable(void)
{//DDRC|=0x01;
//PORTC|=0x01;
ADCSR = 0x00;
}
#pragma interrupt_handler adc_isr:15
void adc_isr(void)
{
//conversion complete, read value (int) using...
adc_value=ADCL; //Read 8 low bits first (important)
adc_value|=(int)ADCH << 8; //read 2 high bits and shift into top byte
ADCSR = 0x00;
ADCSR = 0xC8;
}
int ReadADC( unsigned char channel)
{
int i;
ADMUX = channel; // Select channel
ADCSR |= 0x40; // Start conversion
while (!(ADCSR & 0x10)); // Check if converstion is ready
ADCSR |= 0x10; // Clear Conversion ready flag by setting the bit
i = ADCL; // Read 8 low bits first (important)
i += (int)ADCH << 8; // Read 2 high bits and multiply with 256
return i;
}
//call this routine to initialise all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
timer1_init();
// spi_init();
// twi_init();
uart0_init();
// adc_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x05; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialised
}
void DisplayHexData(unsigned char timerH,unsigned char timerL)
{
unsigned char temptimer1,temptimer2,temptimer3,temptimer4;
temptimer1=timerL%16;
temptimer2=timerL/16;
temptimer3=timerH%16;
temptimer4=timerH/16;
Display_Data_LED(temptimer1,temptimer2,temptimer3,temptimer4);
}
void DisplayData(unsigned int timer)
{
unsigned char temptimer1,temptimer2,temptimer3,temptimer4;
temptimer4=timer/1000;
timer=timer%1000;
temptimer3=timer/100;
timer=timer%100;
temptimer2=timer/10;
temptimer1=timer%10;
Display_Data_LED(temptimer1,temptimer2,temptimer3,temptimer4);
}
void Display_Data_LED(unsigned char Data1, unsigned char Data2,unsigned char Data3,unsigned char Data4)
{unsigned char displaydata,displaydata_2;
switch(Display_Position)
{
case 1: displaydata=DataTab[Data1];
displaydata_2=DataTab[Data1]&0xc0;
PORTD=0x3B|displaydata_2;
PORTB=displaydata;
break;
case 2:displaydata=DataTab[Data2];
displaydata_2=DataTab[Data2]&0xc0;
PORTD=0x37|displaydata_2;
PORTB=displaydata;
break;
case 3: displaydata=DataTab[Data3];
displaydata_2=DataTab[Data3]&0xc0;
PORTD=0x2F|displaydata_2;
PORTB=displaydata;
break;
case 4: displaydata=DataTab[Data4];
displaydata_2=DataTab[Data4]&0xc0;
PORTD=0x1F|displaydata_2;
PORTB=displaydata;
break;
default:
break;
}
}
void DisplayTimer(unsigned int timer)
{
unsigned char temptimer1,temptimer2,temptimer3,temptimer4;
temptimer1=timer%60;
temptimer3=timer/60;
temptimer2=temptimer1/10;
temptimer1=temptimer1%10;
temptimer4=temptimer3/10;
temptimer3=temptimer3%10;
Display_Data_LED(temptimer1,temptimer2,temptimer3,temptimer4);
}
unsigned char Key_Decode(void)
{ Delay(10);
DDRC&=0x79;
PORTC|=0x30;
PORTC&=0x59;
Delay(10);
if (!(PINC & 0x02))
{ return key_inc;}
if (!(PINC & 0x04))
{return key_enter;}
PORTC|=0x30;
PORTC&=0x69;
Delay(10);
if (!(PINC & 0x02))
{return key_dec;}
if (!(PINC & 0x04))
{return key_mode;}
return key_no;
}
void Key_Task(void)
{
switch(key_state)
{
case KBD_IDLE:
{
key_value=key_no;
key_pressed=false;
key_value=Key_Decode();
if(key_value!=key_no)
{
key_state = KBD_DEBOUNCE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -