Hello,
I have a device that I'm trying to communicate with. I have to send 26 command bits along with a clock at 5 MHz to this device in order to get a response back. I wrote some code for the Due that will generate two signals that are close to 5 MHz (about 4.94 MHz) which i believe might be close enough. I am using PWM mode to drive these two signals. One signal is the clock the second is the data.
Here is what i want:
Here is the code i have so far:
void setup ()
{
PMC->PMC_PCER1 |= PMC_PCER1_PID36;
REG_PWM_DIS = PWM_DIS_CHID0|PWM_DIS_CHID2; // disable both pins
REG_PWM_CLK = PWM_CLK_PREA(0)|PWM_CLK_DIVA(1);
REG_PIOC_PDR |= PIO_PDR_P3; // pin 35
REG_PIOC_ABSR |= PIO_PC3B_PWMH0; // pin 35
REG_PWM_CMR0 = PWM_CMR_CPRE_CLKA;
REG_PWM_CPRD0 = 1; //vs 16
REG_PWM_CDTY0 = 0;//100% //vs 8
REG_PIOC_PDR |= PIO_PDR_P7; // pin 39
REG_PIOC_ABSR |= PIO_PC7B_PWMH2; // pin 39
REG_PWM_CMR2 = PWM_CMR_CPRE_CLKA;
REG_PWM_CPRD2 = 16; //vs 1
REG_PWM_CDTY2 = 8; // 50% //vs 0
//REG_PWM_SCM = 5;
//REG_PWM_ENA = PWM_ENA_CHID0|PWM_ENA_CHID2;
}
int cnt = 26;
void loop()
{
while(1)
{
while(cnt)
{
PWM->PWM_ENA = 5;//PWM_ENA_CHID0|PWM_ENA_CHID2;
PWM->PWM_DIS = 5;//PWM_DIS_CHID0|PWM_DIS_CHID2;
--cnt;
}
delay(1000);
cnt = 26;
}
}
Questions:
1. Is there a better way to do this?
2. If i swap the two PWM pins,
ie
REG_PWM_CPRD2 = 1;
REG_PWM_CDTY2 = 0;
and
REG_PWM_CPRD0 = 16;
REG_PWM_CDTY0 = 8;
my frequency drops to ~3MHz, why does that happen?
3. Are there any more optimization techniques i can use to make this faster?
Thank You.