📄 mcu_reg_init_def.c
字号:
/******************************************************************************
*
* Purpose: Microcontroller init Function
*
* Creator: Rob Lansbergen
*
* Version: $Revision: 12 $
*
* File Name: $Workfile: MCU_Reg_Init_Def.c $
*
* Author: Rob Lansbergen
*
* Check in: $Author: Chong.cheeleong $
*
* The information is provided 揳s is?without any express or implied warranty
* of any kind, * including warranties of merchantability, noninfringement of
* intellectual property, or fitness for any particular purpose. In no event sha
* Wireless Sound Solutions and/or its affiliate companies, or its suppliers be
* liable for any damages whatsoever arising out of the use of or inability to
* use the information or the materials. Wireless Sound Solutions, its affiliate
* companies, and its suppliers further do not warrant the accuracy or
* completeness of information, text, graphics, or other items contained within
* materials. Wireless Sound Solutions, Inc., may make changes to materials, or
* the products described within, at any time, without notice.
* ?007 Wireless Sound Solutions. All rights reserved. Wireless Sound Solutions
* STS and STS-wireless are trademarks of Wireless Sound Solutions.
******************************************************************************/
/*
** Include files
** -------------
*/
#include <ina90.h>
#include "defines.h"
#ifdef MCU_ATMEGA128
#define MHZ_8
#else
#define MHZ_1
#endif
//#define MHZ_1
//#define MHZ_8
//#define ENABLE_TXD_DEBUG
/* Reload value for when 8 MHz or 1 MHz is used */
#define RELOAD_OF_TCNT0 (256-(156/4)) /* CLK=4KHz divide by 39 => 100Hz=10mSec */
void Interrupt_every_10millisec(void);
void Low_Batt_Detection(void);
void MCU_Sleep_Powerdown(void);
volatile unsigned char Wait_Timer;
/******************************************************************************
*
* Function Name : MCU_init
*
* Purpose : inititalises mcu
*
* Arguments : none
*
* Return value : none
*
******************************************************************************/
/**************************************************************************************/
void MCU_init (void)
{
char temp;
/* IO ports NC = Not Connected */
/* Direction */
//DDRB = 0xC0; /* CODEC_RESET, ASIC_RESET, SCK, MISO, INT_MOSI, PREV_SW, PLAY_PAUSE_STOP_SW, NEXT_SW */
//PORTB = 0x0F; /* Keep CODEC_RESET low upon power up */
DDRB = 0xC1; /* CODEC_RESET, ASIC_RESET, SCK, MISO, INT_MOSI, PREV_SW, PLAY_PAUSE_STOP_SW, NEXT_SW */
PORTB = 0x0E; /* Keep CODEC_RESET low upon power up */
#ifdef Target_CU
// 7 6 5 4 3 2 1 0
// 0 0 1 1 0 1 0 0 old
DDRC=0x37;
DDRD=0x04;
/* Pull Up */
PORTC=0x08;
PORTD=0xF3;
#else
DDRC = 0x37;
DDRD = 0x20;
//DDRD=0x20; //Allan add PD5 for mute control: from 0x20 to 0x20//
PORTC = 0x08;
PORTD = 0xDF;
//PORTD=0xC3; //Allan add PD5 for mute control: from 0xF3 to 0xC3//
// DDRC = 0x34; /* ADC7, MCU_RESET, SCL, SDA, CONNECT_SW, SYNC_LED, NC, GPIO4 */
// 7 6 5 4 3 2 1 0
// 0 0 1 1 0 1 0 0 old
// 1 1 MU
// DDRC = 0x3F;
// DDRD = 0x00; /* VOL_UP_SW, MU_CU_SW, VOL_DOWN_SW, NC, NC, GPIO3, GPIO2, GPIO1 */
// 7 6 5 4 3 2 1 0
// 0 0 0 0 0 0 0 0 old
// 1 0 0 MU
/* Pull Up */
// PORTC = 0x09;
// 7 6 5 4 3 2 1 0
// 0 0 0 0 1 0 0 1 old
// 0 0 MU
// PORTC = 0x00;
// PORTD = 0xE7;
// 7 6 5 4 3 2 1 0
// 1 1 1 0 0 1 1 1 old
// 0 1 1 MU
#endif
SFIOR = 0x00;
/* MCU control */
MCUCR = 0x00;
temp = MCUCSR & ~0x80;
MCUCSR = temp; /* JTAG disable bit must be written twice within 4 cycles */
MCUCSR = temp;
/* ADC */
ADMUX = 0xC6; /* Internal 2.56V VRef with ext. cap. at AREF pin; default ADC result is right-adjusted; X; Selected analog input ADC6 */
ADCSR = 0x9F; /* ADC enabled; Do not start ADC conversion; Single conversion (no free running) ; Clear ADC interrupt flag; */
/* ADC Conversion Complete Interrupt enabled; */
/* ADC prescaled to divide XTAL freq of 1MHz by a factor of 8 = 125kHz */
/* timer interrupts */
TIMSK = 0x00; /* Clear all timer interrupts */
TIFR = 0xFF; /* Clear all flags by reading them */
/* timers and counters */
ASSR = 0x00; /* Timers clocked from IO-CLK not from XTAL */
TCCR0 = 0x00; /* Normal operation, clock stopped */
TCNT0 = 0x00; /* Counter value = 0 */
TCCR1A = 0x00; /* All pins with output compare, function as IO pins */
TCCR1B = 0x00;
TCNT1H = 0x00;
TCNT1L = 0x00;
ICR1H = 0x00;
ICR1L = 0x00;
TCCR2 = 0x00; /* No clk source, OC2 disconnected */
TCNT2 = 0x00;
OCR2 = 0xFF;
TIMSK = TIMSK | 0x01; /* Interrupt on Overflow */
TIFR = TIFR | 0x01;
TCCR0 = 0x04;
TCNT1 = 2; /* Value = 72 == 5 msec */
/* countval0 = (duration/50); */ /* input in msec, 72 counts == 5 msec, countval counts 5msec steps */
/* Enable interrupts */
SREG = SREG | 0x80;
_SEI();
i2c_init();
/* UART communicates to PC */
UCSRC = 0x06;
/* RX enable, TX enable */
/* UCSRB = 0x08; */
UCSRB = 0x00;
UCSRA = 0x00;
UBRRH = 0x00;
UBRRL = 49; /* 00000001 Baud rate = 38,4k at 8Mhz */
}
/**************************************************************************************/
#pragma vector = ADC_vect
__interrupt void ADCVInterrupt(void)
{
Low_Batt_Detection();
}
#pragma vector = TIMER0_OVF_vect
__interrupt void Timer0OVInterupt(void)
{
TCNT0 = RELOAD_OF_TCNT0; /* 10 millisecond on 1 Mhz */
Wait_Timer++;
Interrupt_every_10millisec();
}
void Wait_For_mSec(int Milisec)
{
if (Milisec > 2550) Milisec = 2550;
if (Milisec < 10) Milisec = 10;
_CLI();
Wait_Timer = 0;
_SEI();
while (Wait_Timer < (Milisec/10))
{
/* wait for X times 10 mSec */
}
}
void Low_Batt_Detection(void)
{
unsigned char hi_byte, lo_byte;
lo_byte = ADCL; /* Read ADC low byte */
hi_byte = ADCH; /* Read ADC high byte. This will allow ADC to update ADCH and ADCL */
/* Codec shuts down and LED blinks when battery level falls below 2.8V (0.675V at ADC6 analog input) */
if ( (lo_byte < 0x0E) && (hi_byte < 0x02) )
{
// Powerdown_Codec();
Sync_Led_Status = LED_LOW_BATT; /* Use blinking LED to inform user of low battery level */
// MCU_Sleep_Powerdown();
}
ADCSR = ADCSR | 0x40; /* Start next conversion */
}
void MCU_Sleep_Powerdown(void)
{
MCUCR = MCUCR | 0xC0; /* Set Sleep to Power-down Mode */
_SLEEP(); /* Enter Sleep */
}
//TX Board ATmega8 DDR PORT ATmeag88 DDR PORT
// PD7 0 1
// PD6 0 1
// PD5 0 1
//1. CONNECT BUTTON Pin26 PC3 0 1 Pin2 PD4 0 1(0)
// PD3 0 0
//4. change LINK_LED Pin25 PC2 1 0 Pin32 PD2 1 0(1)
//5. ASIC_INT Pin15 PB3 0 1 Pin31 PD1 0 1
// PD0 0 1
// 0x04 0xF3
// PC7 0 0
// PC6 0 0
// PC5 1 0
// PC4 1 0
// PC3 0 1
// PC2 1 0
//2. CODEC_\RESET Pin8 PB7 1 0 Pin24 PC1 1 0
//3. ASIC_\RESET Pin7 PB6 1 0 Pin23 PC0 1 0(1)
// 0x37 0x08
/**************************************************************************************/
//RX Board ATmega8 DDR PORT ATmeag88 DDR PORT
// PD7 0 1
// PD6 0 1
//3. LINK_LED Pin25 PC2 1 0 Pin9 PD5 1(0) 0(1)
//2. CONNECT BUTTON Pin26 PC3 0 1 Pin2 PD4 0 1(0)
//1. SWAP_KEY Pin10 PD6 0 1 Pin1 PD3 0 1(0)
// PD2 0 1
//5. ASIC_INT Pin15 PB3 0 1 Pin31 PD1 0 1
// PD0 0 1
// 0x20 0xDF
// PC7 0 0
// PC6 0 0
// PC5 1 0
// PC4 1 0
// //PC3 0 1
// PC3 1 0
// PC2 1 0
//6. CODEC_\RESET Pin8 PB7 1 0 Pin24 PC1 1(0) 0(1)
//4. ASIC_\RESET Pin7 PB6 1 0 Pin23 PC0 1(0) 0
// 0x37 0x08
// 0x3F 0x00
/*Original IO ports NC = Not Connected */
/* Direction */
//DDRB = 0xC0; /* CODEC_RESET, ASIC_RESET, SCK, MISO, INT_MOSI, PREV_SW, PLAY_PAUSE_STOP_SW, NEXT_SW */
//DDRC = 0x34; /* ADC7, MCU_RESET, SCL, SDA, CONNECT_SW, SYNC_LED, NC, GPIO4 */
//DDRD = 0x00; /* VOL_UP_SW, MU_CU_SW, VOL_DOWN_SW, NC, NC, GPIO3, GPIO2, GPIO1 */
/* Pull Up */
//PORTB = 0x0F; /* Keep CODEC_RESET low upon power up */
//PORTC = 0x09;
//PORTD = 0xE7;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -