📄 fm.c
字号:
#include <avr/io.h>
#include <avr/signal.h>
#include <avr/delay.h>
#include <avr/iomacros.h>
//#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/twi.h>
#include <stdio.h>
#include <compat/twi.h>
#define deviceAddr 0x88
#define PTADJUST 0xe0
#define PTFINEADJ 0xd0
// tClk/256 = 32 uS 15625 (3d09 3125 (c35) 25250 (62a2 ) 312.5(138)
#define TI1_H_2s 0xf4
#define TI1_L_2s 0xff
#define TI1_H_1s 0x9d
#define TI1_L_1s 0x5d
#define TI1_H_500ms 0xc2
#define TI1_L_500ms 0xf6
#define TI1_H_100ms 0xf3
#define TI1_L_100ms 0xca
//#define TI1_H_3s 0xfe
//#define TI1_L_3s 0xc7
#define TI1_H_55ms 0xf9
#define TI1_L_55ms 0x49 // 0xe5
#define TI1_H_6ms 0xff
#define TI1_L_6ms 0xe2 // 0x44 for 6ms. 0x25 for 7ms 0x35 for 6.5ms
#define TI0_4ms 0x82
#define TI0_6ms 0xa2
//#define TI0_6ms 0x44
//#define TI0_100ms 0x44
#define DISPLAY_NUMBER_0 ~0x3f
#define DISPLAY_NUMBER_1 ~0x06
#define DISPLAY_NUMBER_2 ~0x5b
#define DISPLAY_NUMBER_3 ~0x4f
#define DISPLAY_NUMBER_4 ~0x66
#define DISPLAY_NUMBER_5 ~0x6d
#define DISPLAY_NUMBER_6 ~0x7d // ??
#define DISPLAY_NUMBER_7 ~0x07 // ??
#define DISPLAY_NUMBER_8 ~0x7f
#define DISPLAY_NUMBER_9 ~0x6f
#define DISPLAY_NUMBER_C ~0x39
#define DISPLAY_NUMBER_H ~0x76
#define DISPLAY_NUMBER_NO 0xff
#define LED_FLASH_FREQ 25 // 0x05
//管脚定义
#define FREQENCE_ADD PC0
#define FREQENCE_MIN PC1
#define SET_MODE PC2
#define BH1415_CS PC3
#define BH1415_DATA PC4
#define BH1415_CLOCK PC5
#define BH1415_MUTE PB4
#define LED_COM1 PB0
#define LED_COM2 PB1
#define LED_COM3 PB2
#define LED_COM4 PB3
#define SET_LED_COM1_HIGH() PORTB|= (1<<LED_COM1)
#define SET_LED_COM2_HIGH() PORTB|= (1<<LED_COM2)
#define SET_LED_COM3_HIGH() PORTB|= (1<<LED_COM3)
#define SET_LED_COM4_HIGH() PORTB|= (1<<LED_COM4)
#define SET_LED_COM1_LOW() PORTB&=~(1<<LED_COM1)
#define SET_LED_COM2_LOW() PORTB&=~(1<<LED_COM2)
#define SET_LED_COM3_LOW() PORTB&=~(1<<LED_COM3)
#define SET_LED_COM4_LOW() PORTB&=~(1<<LED_COM4)
#define SET_BH1415_CE_HIGH() PORTC|= (1<<BH1415_CS)
#define SET_BH1415_DATA_HIGH() PORTC|= (1<<BH1415_DATA)
#define SET_BH1415_CLOCK_HIGH() PORTC|= (1<<BH1415_CLOCK)
#define SET_BH1415_MUTE_HIGH() PORTB|= (1<<BH1415_MUTE)
#define SET_BH1415_CE_LOW() PORTC&=~(1<<BH1415_CS)
#define SET_BH1415_DATA_LOW() PORTC&=~(1<<BH1415_DATA)
#define SET_BH1415_CLOCK_LOW() PORTC&=~(1<<BH1415_CLOCK)
#define SET_BH1415_MUTE_LOW() PORTB&=~(1<<BH1415_MUTE)
#define BH1415_STORE_FREQ 20
#define BH1415_STORE_CHA0 0
#define BH1415_STORE_CHA1 2
#define BH1415_STORE_CHA2 4
#define BH1415_STORE_CHA3 6
#define BH1415_STORE_CHA4 8
#define BH1415_STORE_CHA5 10
#define BH1415_STORE_CHA6 12
#define BH1415_STORE_CHA7 14
#define BH1415_STORE_CHA8 16
#define BH1415_STORE_CHA9 18
#define sys_status_mode 0x01
#define sys_status_channel 0x05
#define sys_status_frequence 0x06
// define for mcu
volatile unsigned char bh1415_time_count,led_is_flash,led_flash_count,display_freq;
volatile unsigned char system_status,Timer1_int_count;
unsigned int bh1415_frequence,past_frequence;
unsigned char display_chan_value,set_channel_value;
unsigned char testport;
void led_Chan_display(unsigned char cutoff_display);
void led_display(unsigned char display_value,unsigned char display_dot);
void led_Feq_display(unsigned char cutoff_display);
void save_bh1415_Feq(void);
void read_bh1415_Feq(void);
void read_bh1415_Channel(void);
void save_channel_Feq(void);
void BH1415_Feq_MIN(void);
void BH1415_channel_MIN(void);
void BH1415_channel_ADD(void);
void BH1415_Feq_ADD(void);
void write_bh1415(void);
void enabletimer0(void);
void disabletimer0(void);
void disabletimer0_count(void);
void disabletimer1(void);
//void enabletimer1(unsigned char timer1_init_value);
void enabletimer1(void);
void allow_all_interrupt(void);
void clear_all_interrupt(void);
unsigned int eeprom_read_word(unsigned char eep_read_Address);
void eeprom_write_word( unsigned char eep_write_Address,unsigned int eep_write_Data);
int main(void)
{
// outp(GICR,0); // disable ext. interrupt
unsigned char main_temp0;
DDRD=0xff;
PORTD=0xff; // if the port is input,enable the pull up resiste,or if the port is output, them it is high level
// DDRB=0x00; // donn't use them , so them is input ,and pull up is enable
DDRB=0xff; // PB0,PB1,PB2,PB3,is input set them as 0 ,and pull up is enable,others is output and set them as 1
// DDRC=0x00; // 1,is output,0 is input
PORTB=0xff; // pb0 is int0,
DDRC=0x38;
PORTC=0xff;
system_status=0;
display_freq=1;
clear_all_interrupt();
// USART_INIT(51); // 8 12 16 25 51 103 // 8比较好,但第三代的IPOD工作不稳定,25对它好一些的
for(main_temp0=0;main_temp0<20;main_temp0++)
{
_delay_ms(30);
}
read_bh1415_Feq();
past_frequence=bh1415_frequence;
enabletimer0();
while(1)
{
if(bh1415_frequence!=past_frequence)
{
past_frequence=bh1415_frequence;
save_bh1415_Feq();
}
_delay_ms(10);
testport=PINC;
if((testport&(1<<SET_MODE))==0)
{
main_temp0=0;
waiting_mode_key_high:
_delay_ms(10);
testport=PINC;
if((testport&(1<<SET_MODE))==0)
{
main_temp0++;
if(main_temp0>220)
{
main_temp0=220;
}
goto waiting_mode_key_high;
}
if(main_temp0>200)
{ // 进入修改和存储频道的操作
if(system_status==sys_status_frequence)
{ // 保存上次频道和频率值,并进入下一次的存储设置
save_channel_Feq();
// 如果用两套频道值,一个是发射的,另一个是设置的话,这里就不需要其它的程序
// 否则,在这里把两套值同意成一个频道值
// display_chan_value=set_channel_value;
system_status=sys_status_channel;
set_channel_value=0;
}
else
{ // 进入存储设置
if(system_status!=sys_status_channel)
{
set_channel_value=0;
}
system_status=sys_status_channel;
}
led_is_flash=1;
display_freq=0;
enabletimer1();
}
else
{ // 调用频道值
if(system_status==sys_status_frequence)
{ // 保存上次频道和频率值,并进入下一次的存储设置
save_channel_Feq();
// 如果用两套频道值,一个是发射的,另一个是设置的话,这里就不需要其它的程序
// 否则,在这里把两套值同意成一个频道值
set_channel_value=0;
system_status=sys_status_mode;
led_is_flash=0;
display_freq=0;
Timer1_int_count=0;
enabletimer1();
}
else
{
display_chan_value++;
if(display_chan_value>9)
{
display_chan_value=0;
}
read_bh1415_Channel();
// led_is_flash=1; // 显示的数字闪烁
led_is_flash=0; // 显示的数字闪烁
system_status=sys_status_mode; // 显示的是频道值,而不是设置的频道值
display_freq=0; // 显示 频道值,而不是频率值
enabletimer1();
}
for(main_temp0=0;main_temp0<8;main_temp0++)
{
_delay_ms(30);
}
}
}
if((testport&(1<<FREQENCE_ADD))==0)
{
/*
if(system_status==sys_status_mode)
{ // 改变频道值,并调用相关的频率值
BH1415_channel_ADD();
}
else
*/
if(system_status==sys_status_channel)
{ // 改变设置的频道值
set_channel_value++;
if(set_channel_value>9)
{
set_channel_value=0;
}
enabletimer1();
Timer1_int_count=0;
}
else
{ // 改变设置的频率值
BH1415_Feq_ADD();
if(system_status==sys_status_frequence)
{
Timer1_int_count=0;
enabletimer1();
}
}
for(main_temp0=0;main_temp0<7;main_temp0++)
{
_delay_ms(30);
}
}
else if((testport&(1<<FREQENCE_MIN))==0)
{
/*
if(system_status==sys_status_mode)
{ // 改变频道值,并调用相关的频率值
BH1415_channel_MIN();
}
else
*/
if(system_status==sys_status_channel)
{ // 改变设置的频道值
if(set_channel_value>0)
{
set_channel_value--;
}
else
{
set_channel_value=9;
}
enabletimer1();
Timer1_int_count=0;
}
else
{ // 改变设置的频率值
BH1415_Feq_MIN();
if(system_status==sys_status_frequence)
{
Timer1_int_count=0;
enabletimer1();
}
}
for(main_temp0=0;main_temp0<7;main_temp0++)
{
_delay_ms(30);
}
}
}
}
void enabletimer0(void)
{
allow_all_interrupt();
// 清定时器0的中断标志
TIMSK0|=1<<TOIE0; // Enable timer0 interrupt
TCNT0=TI0_6ms;
TCCR0B=1<<CS02; // Prescaler /256 (1,0,0) 32us
}
void disabletimer0_count(void)
{
// timer0_source(STOP);
TCCR0B&=~(1<<CS02|1<<CS01|1<<CS00); // 0x00;
}
void clear_all_interrupt(void)
{
// cli();
SREG&=0x7f;
// disableEXT1();
}
void allow_all_interrupt(void)
{
SREG|=0x80;
}
//void enabletimer1(unsigned char timer1_init_value)
void enabletimer1(void)
{ // TCNT1
TIMSK1|=1<<TOIE1; // Enable timer1 interrupt
TCNT1H=TI1_H_2s; // Load counter value hi
TCNT1L=TI1_L_2s; // Load counter value lo
TCCR1A=0x00;
// TCCR1B=0x00;
TCCR1B=1<<CS12; // Prescaler /256 (1,0,0) 32us
Timer1_int_count=0;
}
void disabletimer1(void)
{
// cbi(TIMSK, TOIE1); // disable timer1 interrupt
// TIMSK1&=~(1<<TOIE1);
TCCR1B=0x00;
}
void write_bh1415(void)
{
// 01001 T1,T0=01,PD1PD0=00,MONO=1
unsigned char bh1415_temp;
unsigned int bh1415_send_data;
SET_BH1415_CE_LOW();
SET_BH1415_MUTE_HIGH();
SET_BH1415_CLOCK_LOW();
_delay_us(5);
SET_BH1415_CE_HIGH();
bh1415_send_data=bh1415_frequence&0x07FF;
bh1415_send_data=bh1415_send_data|0x4800;
_delay_us(5);
for(bh1415_temp=0;bh1415_temp<16;bh1415_temp++)
{
if((bh1415_send_data&0x01)==0x00)
{
SET_BH1415_DATA_LOW();
}
else
{
SET_BH1415_DATA_HIGH();
}
_delay_us(5);
SET_BH1415_CLOCK_HIGH();
bh1415_send_data>>=1;
_delay_us(5);
SET_BH1415_CLOCK_LOW();
}
_delay_us(5);
SET_BH1415_CE_LOW();
_delay_ms(10);
SET_BH1415_MUTE_LOW();
}
void BH1415_Feq_ADD(void)
{
if(bh1415_frequence<1079)
{
bh1415_frequence++;
}
else
{
bh1415_frequence=881;
}
write_bh1415();
}
void BH1415_Feq_MIN(void)
{
if(bh1415_frequence>881)
{
bh1415_frequence--;
}
else
{
bh1415_frequence=1079;
}
write_bh1415();
}
void BH1415_channel_ADD(void)
{
display_chan_value++;
if(display_chan_value>9)
{
display_chan_value=0;
}
read_bh1415_Channel();
write_bh1415();
}
void BH1415_channel_MIN(void)
{
if(display_chan_value>0)
{
display_chan_value--;
}
else
{
display_chan_value=9;
}
read_bh1415_Channel();
write_bh1415();
}
void save_bh1415_Feq(void)
{
eeprom_write_word(BH1415_STORE_FREQ,bh1415_frequence);
}
void read_bh1415_Feq(void)
{
bh1415_frequence=eeprom_read_word(BH1415_STORE_FREQ);
if(bh1415_frequence>1079)
{
bh1415_frequence=881;
}
if(bh1415_frequence<881)
{
bh1415_frequence=881;
}
write_bh1415();
}
void read_bh1415_Channel(void)
{
bh1415_frequence=eeprom_read_word(2*display_chan_value);
if(bh1415_frequence>1079)
{
bh1415_frequence=881;
}
if(bh1415_frequence<881)
{
bh1415_frequence=881;
}
eeprom_write_word(2*display_chan_value,bh1415_frequence);
write_bh1415();
}
void save_channel_Feq(void)
{
display_chan_value=set_channel_value;
eeprom_write_word(2*display_chan_value,bh1415_frequence);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -