وبعد منتطر دریافت کد از کیپد میشود اگر کلید فشاری 4،8،12،16 بود شکل موج خروجی راتغییر میدهد اگر هم کلید 1 فشرده شد موج خروجی شینوسی با فرکانس 100k در خروجی باید ایجاد شود
* this code write wait MikroC avr by aliasghar hashemi for Deriver AD9833 wait
keypad input
hashemi911t@gamil.com,
09119109304*/
// Keypad module connections
char keypadPort at PORTD;
char keypadPort_Direction at DDRD;
// End Keypad module connections
//**********************initialization AD9833 funtion********************
void initAD9833 (void)
{
//CONT REG Reset=1&HLB=0 0b0010 0001 0000 0000
PORTB.B4=0;
SPI1_Write(0x21);
SPI1_Write(0x00);
PORTB.B4=1;
// write LSB freq reg0 0b 01 101001 11100011 out 1k
PORTB.B4=0;
SPI1_Write(0x69);// D13 - D8 represents 14 LSBs
SPI1_Write(0xE3); //D7 - D0
PORTB.B4=1;
// write MSB freq reg0 0b01 000000 00000000
PORTB.B4=0;
SPI1_Write(0x64);// D13 - D8 represents 14 LSBs
SPI1_Write(0x00); //D7 - D0
PORTB.B4=1;
// phase reg 0b11000000 00000000
PORTB.B4=0;
SPI1_Write(0xC0); // D13 - D8 represents 14 MSBs
SPI1_Write(0x00); // D7 - D0
PORTB.B4=1;
// CONT REG reset=0, select felect&Pselect 0b0010 0000 0000 0000
PORTB.B4=0;
SPI1_Write(0x20); // D15 - D8, Writing to Control Regsiter
SPI1_Write(0x00); // Setting for Sine Wave Output
PORTB.B4=1;
}
// *****************selective out put wave*********************
void out(int kp)
{
if (kp==4)
{
PORTB.B4=0;
SPI1_Write(0x20); // 0b0010 0000 0000 0000 sin
SPI1_Write(0x00);
PORTB.B4=1;
Delay_us(100);
}
else if (kp==8)
{
PORTB.B4=0;
SPI1_Write(0x20); // 0b0010 0000 0000 0010 Tri
SPI1_Write(0x02);
PORTB.B4=1;
Delay_us(100);
}
else if (kp==12)
{
PORTB.B4=0;
SPI1_Write(0x20); // 0b0010 0000 0010 1000 DAC DATA MSB
SPI1_Write(0x28);
PORTB.B4=1;
Delay_us(100);
}
else if (kp==16)
{
PORTB.B4=0;
SPI1_Write(0x20); // 0b0010 0000 0010 0000 DAC DATA MSB/2
SPI1_Write(0x20);
PORTB.B4=1;
Delay_us(100);
}
}
//*******************write frq AD9833******************************
void write_freq(int kp )
{
if(kp==1)
{
PORTB.B4=0; // write to cort reg 0010 0000 0000 0000
SPI_Write(0x02);
SPI_Write(0x00);
PORTB.B4=1;
PORTB.B4=0;// write to FREQ REG fout=0.00931*freg
SPI_Write(0x43); // 01000011 11101000
SPI_Write(0xE8);
PORTB.B4=1;
PORTB.B4=0;
SPI_Write(0x40);
SPI_Write(0x00);
PORTB.B4=1;
PORTB.B4=0; // write to phasr reg
SPI_Write(0xc0);
SPI_Write(0x00);
PORTB.B4=1;
}
Delay_us(100);
}
void main() {
//char keybuf[5];
int kp=0;
DDRB.B4=1; //SS pin defide to outpot
DDRB.B5=1; //MOSI pin defide to outpot
DDRB.B7=1; //SCK pin defide to outpot
PORTB.B4=1; //SS high
SPI1_Init();
Keypad_Init();
initAD9833();
Delay_us(100);
while (1)
{
do
{
kp=Keypad_Key_Click();
}while(!kp);
write_freq(kp);
out(kp);
}