📄 main.c
字号:
// Compiler : IAR EWAVR
// Target : M8
// Crystal: 8Mhz
// Date :2006-12-01
#include <iom8.h>
#include <inavr.h>
#include <iomacro.h>
#include "main.h"
#include "spi.c"
#include "usart.c"
//#define _EEPUT(ADR,VAL) (*((unsigned char _eeprom *)ADR) = VAL) //write the eeprom
//#define _EEGET(VAL,ADR) (VAR = *((unsigned char _eeprom *)ADR)) //read the eeprom
void port_init(void) //I/0 ports initialize
{
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0xD0; //PORD6,7 is out
}
void watchdog_init(void) //WATCHDOG initialize
{
__watchdog_reset(); //this prevents a timout on enabling
WDTCR = 0x08; //WATCHDOG ENABLED - dont forget to issue WDRs
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void delay(uchar time_num) //general delay function
{
uchar i;
while(time_num--)
{
i = 200;
while(i--)
;
}
}
void delay_adc(uchar time_num) //delay function for adc convertion
{
while(time_num--)
;
}
//******************************************************
//Encoder
//external interrupt initialize
void ext_interrupt_init()
{
;
}
//ADC initialize
// Conversion time: 3uS
void adc_init(void) //ADC initialize
{
ADCSR = 0x00; //disable adc
ADMUX = 0x20; //AREF reference;left adjust;select adc input 0
ACSR = 0x80; //Analog Comparator Disable
ADCSR = 0x81; //ADC enable;division factor :2;ADC interrupt disable
}
//*************************************************************************
//ADC result deal with
//every time see about if the ADC convertion complete
//afer delay time over
void adc_result_deal()
{
uchar i = 0;
do
{
ADMUX &= 0xF0;
ADMUX |= i; //change the ADC channel
SET_BIT(ADCSR,6); //start ADC convertion
delay_adc(50); //wait for ADC convertion complete
if(ADCSR & 0x10)
{
; //save ADC result
}
SET_BIT(ADCSR,6); //start ADC convertion
delay_adc(50); //wait for ADC convertion complete
if(ADCSR & 0x10)
{
adc_result[i++] = ADCH; //save ADC result
}
}while(i <= 3);
}
//*************************************************************************
void Timer0_init() //timer0 initialize
{
TCCR0 = 0x00; //stop timer0
TCNT0 = 0x9C; //100 uSEC time
TCCR0 = 0x02; //clock select clkI/8/(from prescaler) start timer
}
//Timer0 interrupt server
#pragma vector = TIMER0_OVF_vect //Timer0 interrupt server program
__interrupt void timer0_ovf_isr(void)
{
TCNT0 = 0x9C; //reload counter value
LED_CONTROL_FLAG = 0x01;
}
//encoder scan ,use the external interrupt pins
//
void main()
{
uchar temp_data;
__disable_interrupt(); //disable interrupt
dsp_boot_wait(); //wait for DSP bootloader relate port are all Hi-Z
//while((PIND & 0x20) == 0x20); //if pind.5 is high wait until "XF" is low
port_init(); //port initialize
//watchdog_init(); //watchdog initialize
//Timer0_init(); //Timer0 initialize
adc_init(); //ADC initialize
MCUCR = 0x00; //MCU control register
TIMSK = 0x01; //Timer0 interrupt enable
uart0_init();
SPI_init();
P_uart_data = uart_ping_data;
//eep_program_enable(); //initilize AT25128,program enable
__enable_interrupt(); //interrupt enable
//SET_BIT(PORT_SPI,SS); //SS is high
//uart_rec_data =0x00;
status_flag.at_all = 0x00;
status_flag.bit0 = 1;
SPI_once_end_flag = 0x01; //initilize the flag,for the first writing buffer
while(1)
{
__watchdog_reset(); //reset watchdog
adc_result_deal();
//***********write the eeprom******************************
if(adc_result[2] > 0x80)
{
put_spi_char();
if(SPI_op_code == 0x00) //the array first data transmit start
{
if((SPI_Rstatus_flag == 0x00) || ((SPI_status & 0x01) == 0x01)) //not read register or eep busy
{
SET_BIT(PORT_SPI,SS); //
SPI_Rstatus_flag = 0x01; //set flag for read the status register
CLR_BIT(PORT_SPI,SS); //pull down the SS
SPDR = RDSR; //read the status
}
if(((SPI_status & 0x01) == 0x00) && (SPI_Rstatus_flag == 3)) //eeprom in ready state
{
if(SPI_end_flag == 0x00)
{
SET_BIT(PORT_SPI,SS); //pull up SS
if(SPI_address_count == 64)
{
SPI_address_count = 0x00; //clear the data num counter
temp_data = SPI_tx_addl;
SPI_tx_addl += SPI_tx_counter; //adjust the write address
if(SPI_tx_addl < temp_data)
{
SPI_tx_addh += 1;
}
SET_BIT(SPCR,7); //Enable SPI interrupt
}
CLR_BIT(PORT_SPI,SS); //pull down the SS
SPDR = WREN; //set write enable latch
}
else //send programming protect code
{
SET_BIT(PORT_SPI,SS); //pull up SS
CLR_BIT(PORT_SPI,SS); //pull down the SS
SPDR = WREN; //
}
}
}
}
//**********read the eeprom for test***********************
else
SPI_rx_flag = 1;
if(SPI_rx_flag == 0x01)
{
if(SPI_rx_counter == 0x00) //the array first data transmit start
{
SET_BIT(PORT_SPI,SS); //
CLR_BIT(PORT_SPI,SS); //pull down the SS
SPDR = READ; //set write enable latch
}
if(SPI_rx_counter == 36)
{
SET_BIT(PORT_SPI,SS); //
CLR_BIT(PORT_SPI,SS); //pull down the SS
SPDR = RDSR; //read status register
}
}
//**********************************************************
if(uart_ping_data[0] == 0x5A)
{
usart_transmit(SPI_rx_buffer); //transmit data
SPI_rx_counter = 0x00;
uart_ping_data[0] = 0x00; //transmit stop
}
if(SPI_end_flag == 0x01) //
{
SET_BIT(PORTD,7);
SET_BIT(PORTD,6);
}
if(adc_result[2] < 0x80)
{
SET_BIT(PORTD,7);
CLR_BIT(PORTD,6);
}
delay_adc(200);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -