#define F_CPU 1000000UL #include //standard include for ATMega16 #include #include #include #include #define sbi(x,y) x |= _BV(y) //set bit - using bitwise OR operator #define cbi(x,y) x &= ~(_BV(y)) //clear bit - using bitwise AND operator #define tbi(x,y) x ^= _BV(y) //toggle bit - using bitwise XOR operator #define is_high(x,y) (x & _BV(y) == _BV(y)) //check if the y'th bit of register 'x' is high ... test if its AND with 1 is 1 #define F_OSC 100000000.0 #define HOME 0x02 // input pins #define BAND2 PD7 #define BAND1 PD6 #define BAND0 PD5 #define MODE PD4 #define ROTINT PD3 #define ROT PD2 #define STEP PD1 #define CWSW PD0 // LCD pins #define LCD_D7 PB7 #define LCD_D6 PB6 #define LCD_D5 PB5 #define LCD_D4 PB4 #define LCD_RS PB3 #define LCD_E PB2 // AD9850 pins #define D7 PB7 #define W_CLK PB1 #define FQ_UP PB0 void initInterrupts(void); void startTimer(); void updateDDS(); void display(); void hex2bcd(); void lcdString(char *s); void initLcd(); void lcdCmd(char c); void lcdData(char c); void setBandMode(); int test = 0; int timeout = 0; int enc = 0; long int freqlow[6] = {3500000,7000000,10100000,14000000,21000000,28000000,}; long int freqhigh[6] = {3800000,7200000,10150000,14350000,21450000,29700000,}; char str[16]; // Interrupt service routine INT1 ISR(INT1_vect) { // read PD5 if (PIND & _BV(ROT)) enc++; else enc--; } // Interrupt service routine Timer1 ISR(TIMER1_OVF_vect) { timeout = 1; // tbi(PORTB, PB7); // toggle PB7 for test } void startTimer() { TCNT1 = 65536-1953; // 2 sec period timeout = 0; } void initInterrupts(void) { // PD3 (INT1) and PD5 input // DDRD = 0x00; // Enable the pull-up resistors // PORTD |= (1<>= 1; } // output phase = 0; for (i=0; i<8; i++) { cbi(PORTB,D7); sbi(PORTB, W_CLK); _delay_us(2); cbi(PORTB, W_CLK); } sbi(PORTB, FQ_UP); _delay_us(2); cbi(PORTB, FQ_UP); } void hex2bcd(long int f) { long int x = 10000000; int i; char d; for (i=0; i<8; i++) { d = 0; for (;;) { f -= x; if (f < 0) break; d++; } str[i] = d + 0x30; f += x; x = x/10; } } void display(long int f) { int i, pos, leading = 1; char c; hex2bcd(f); // cursor to pos 0 pos = 0; lcdCmd(0x80 + pos); _delay_ms(10); for (i=0; i<8; i++) { if (pos == 8) { lcdCmd(0xc0); } c = str[i]; if (i==2 || i==5) { lcdData('.'); pos++; } if (c == '0' && leading) { lcdData(' '); pos++; } else { lcdData(c); pos++; leading = 0; } } } void displayStep(int s) { lcdCmd(0xc7); _delay_ms(10); switch (s) { case 10: lcdData(0x2d); break; case 100: lcdData(0x3d); break; case 1000: lcdData(0x11); break; } _delay_ms(10); } void displayMode(int mode) { lcdCmd(0xc3); _delay_ms(10); if (mode & 0x01) lcdString("CW "); else if (mode & 0x10) lcdString("USB"); else lcdString("LSB"); } void lcdString(char *s) { while (*s) lcdData(*s++); } void initLcd() { // 4 bits mode lcdCmd(0x20); _delay_ms(10); lcdCmd(0x20); _delay_ms(10); // cursor shift right, text no shift lcdCmd(0x14); _delay_ms(10); // display on, no cursor, no blink lcdCmd(0x0c); _delay_ms(10); // shift mode lcdCmd(0x06); _delay_ms(10); // home lcdCmd(0x02); _delay_ms(10); // clear display lcdCmd(0x01); _delay_ms(10); } void lcdCmd(char c) { char t; t = c & 0xf0; PORTB = t; sbi(PORTB, LCD_E); _delay_us(1); cbi(PORTB, LCD_E); _delay_us(100); c <<= 4; c &= 0xf0; PORTB = c; sbi(PORTB, LCD_E); _delay_us(1); cbi(PORTB, LCD_E); _delay_us(100); } void lcdData(char c) { char t; t = c & 0xf0; t |= (1<>BAND0; tmp &= 0x07; tmp ^= 0x07; if (tmp != band) { if (band != -1) { // save current frequency current band eeprom_write_dword((unsigned long int *)(band<<2), freq); } band = tmp; // read last frequency for the new band from eeprom freq = eeprom_read_dword((unsigned long int *)(band<<2)); // within band limits if (freq < freqlow[band] || freq > freqhigh[band]) { freq = freqlow[band]; } // update freq display(freq); updateDDS(freq + mf); } // step switch pressed? if ( (PIND & (1<0) { freq += step; enc--; } if (enc<0) { freq -= step; enc++; } display(freq); updateDDS(freq + mf); startTimer(); } } return 0; }