原创 FII1256程序实例

2008-12-8 00:26 2251 7 7 分类: MCU/ 嵌入式

/*****************************************************
Project : FII1256高频头程序
Version :
Date    : 2008-10-1
Author  : 姬瑞江                
Company : 四川西江电子                      
Comments:
Chip type           : ATmega8L
Program type        : Application
Clock frequency     : 8.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 256
*****************************************************/


#include <mega8.h>
#include <delay.h>
#include "eeprom.h"
#include "fi1256ful.c"


unsigned char   ShuiDing;
unsigned char   KEY_DATA,KEY_INT,KEY_Intem;
unsigned char  TunChan_Z,TunChan;
unsigned int TunData;


 


#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7



void CPU_init(void);
void watchdog_init(void);


#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)


// USART Receiver buffer
#define RX_BUFFER_SIZE 8
char rx_buffer[RX_BUFFER_SIZE];


#if RX_BUFFER_SIZE<256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif


// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;


// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
   {
   rx_buffer[rx_wr_index]=data;
   if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
   if (++rx_counter == RX_BUFFER_SIZE)
      {
      rx_counter=0;
      rx_buffer_overflow=1;
      };
   };
}


#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif


// USART Transmitter buffer
#define TX_BUFFER_SIZE 8
char tx_buffer[TX_BUFFER_SIZE];


#if TX_BUFFER_SIZE<256
unsigned char tx_wr_index,tx_rd_index,tx_counter;
#else
unsigned int tx_wr_index,tx_rd_index,tx_counter;
#endif


// USART Transmitter interrupt service routine
interrupt [USART_TXC] void usart_tx_isr(void)
{
if (tx_counter)
   {
   --tx_counter;
   UDR="tx"_buffer[tx_rd_index];
   if (++tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0;
   };
}


#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while (tx_counter == TX_BUFFER_SIZE);
#asm("cli")
if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0))
   {
   tx_buffer[tx_wr_index]=c;
   if (++tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;
   ++tx_counter;
   }
else
   UDR="c";
#asm("sei")
}
#pragma used-
#endif


// Standard Input/Output functions
#include <stdio.h>


// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
TCNT1H = 0xE1; //setup
TCNT1L = 0x7C;       
 #asm("WDR"); //喂狗
 if(LED0I == 0)
    {
     LED0O = 1;
    
    }
 else
   {
    if((Key0==0)&&(KEY_INT == 0))
     {
      delay_us(3);
      if(Key0==0)
        {
         KEY_DATA++;
         if(KEY_DATA>8)
          KEY_DATA=8;
         KEY_Intem = 1;
        }
     }
    if((Key0==1)&&(KEY_Intem != 0)&&(KEY_DATA!=0))
      {
       KEY_INT = 1;   ////按键有效
       KEY_Intem = 0;
      }
     LED0O = 0;
    
   }
}


// Declare your global variables here


void main(void)
{
// Declare your local variables here
unsigned char  Temp,TunTemp;
unsigned int Datatemp;
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=P State1=P State0=P
PORTB=0x07;
DDRB=0x00;


// Port C initialization
// Func6=In Func5=Out Func4=Out Func3=Out Func2=In Func1=In Func0=In
// State6=T State5=1 State4=1 State3=1 State2=T State1=T State0=T
PORTC=0x38;
DDRC=0x38;


// Port D initialization
// Func7=Out Func6=Out Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=1 State6=1 State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0xC0;
DDRD=0xC2;


// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;
/*
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 7.813 kHz
// Mode: Normal top="FFFFh"
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x05;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
*/


//TIMER1 initialize - prescale:1024
// WGM: 0) Normal, TOP="0xFFFF"
// desired value: 1Hz
// actual value:  1.000Hz (0.0%)
 TCCR1B = 0x00; //stop
 TCNT1H = 0xE1; //setup
 TCNT1L = 0x7C;
 OCR1AH = 0x1E;
 OCR1AL = 0x84;
 OCR1BH = 0x1E;
 OCR1BL = 0x84;
 ICR1H  = 0x1E;
 ICR1L  = 0x84;
 TCCR1A = 0x00;
 TCCR1B = 0x05; //start Timer
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top="FFh"
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;


// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;


// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x04;


// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0xD8;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;


// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;


// Global enable interrupts



TunChan_Z = TunChan = 0;
TunData = 1330;
CPU_init();


#asm("sei")  


if(TunChan_Z!=0)
  {
   TunChan = EEPROM_read(9);
   TunData = EEPROM_read2(10+TunChan*2); 
   MTunChan(TunData);
   Datatemp = TunData;
  }
 else
  {
   KEY_INT = 1;
   KEY_DATA = 8;
  }
while (1)
      {
       if(KEY_INT!=0)
        {
       
       
        if(KEY_DATA>7)
         {
         KEY_DATA = 0; 
         LED2O = 0;
       //  TunTemp = 20;
        TunData = 1330;
        TunChan_Z = 0;
        while (1)
          {


         MTunChan(TunData);
         delay_ms(30);
         ShuiDing   = ReadChan()&0x07;
         if(ShuiDing == 0)
          {
           TunData++;
          }
        else
          {
          // TunTemp = 30;
           delay_ms(30);
           ShuiDing   = ReadChan()&0x07;
           if(ShuiDing==2)
              {
                EEPROM_write2(10+TunChan_Z*2,TunData); 
                TunChan_Z++;
                TunData = TunData+63;
              }
            if(ShuiDing==1)
              {
                EEPROM_write2(10+TunChan_Z*2,TunData+1); 
                TunChan_Z++;
                TunData = TunData+63;
              }
            if(ShuiDing==3)
              {
                EEPROM_write2(10+TunChan_Z*2,TunData-1); 
                TunChan_Z++;
                TunData = TunData+63;
              }
           TunData++;
           }
        
     if((TunData>14688)||(TunChan_Z>200)||(Key0 == 0))
         {
         //  MTunChan(EEPROM_read2(10+TunChan*2));
           break;
         }
             
          }
          EEPROM_write(8,TunChan_Z);
          MTunChan(EEPROM_read2(10+TunChan*2));
          Datatemp = TunData;
          LED2O = 1;
        }
        if((KEY_DATA == 1)||(KEY_DATA == 2))
         {
           KEY_DATA = 0; 
           LED2O = 0;
           TunChan++;
           if(TunChan>TunChan_Z)
             TunChan = 0;
           EEPROM_write(9,TunChan);
            MTunChan(EEPROM_read2(10+TunChan*2));
            LED2O = 1;
         }
       if((KEY_DATA >2)&&(KEY_DATA < 6))
         {
           KEY_DATA = 0; 
           LED2O = 0;
           TunChan--;
           if(TunChan>TunChan_Z)
             TunChan = TunChan_Z;
           EEPROM_write(9,TunChan);
            MTunChan(EEPROM_read2(10+TunChan*2));
            LED2O = 1;
         }
      KEY_INT = 0;
    }
    else          ////频率跟踪校正
     {
      delay_ms(1000);
      
         ShuiDing   = ReadChan()&0x07;
       
         putchar(ShuiDing);
        
         if(ShuiDing != 2)
          {
          // ShuiDing   = ReadChan()&0x07;
           if((ShuiDing == 1)||(ShuiDing == 3))
              {
                if(ShuiDing == 1)
                  Datatemp+1;
                else
                 Datatemp-1;
              }
             else
              {
               TunChan = EEPROM_read(9);
               TunData = EEPROM_read2(10+TunChan*2); 
             //  MTunChan(TunData);
               Datatemp = TunData;
              }
           MTunChan(Datatemp);
         }


     }


      };
}


 


void CPU_init(void)
        {
          TunChan_Z = EEPROM_read(8);
          TunChan = EEPROM_read(9);
          if(TunChan_Z>200)
            {
             TunChan_Z = 0;
             EEPROM_write(8,TunChan_Z);
             EEPROM_write(9,TunChan_Z);
             EEPROM_write2(10,1330);
            }
        }
       
void watchdog_init(void)
{
 #asm("WDR"); //喂狗
 WDTCR = 0x1F; //
 WDTCR = 0x0F; //WATCHDOG ENABLED - dont forget to issue WDRs
}
rar

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
7
关闭 站长推荐上一条 /3 下一条