I did not know what appropriate forum to post it on..cc CodingBadly.. and sometimes you have to be selfish to be selfless..so i guess its ok.
Can you tell us:
1 ) Do you want to use a timer for 125 kHz output. That is possible. - PWM generation to be precise. I tried doing it and created a variable to store the duration -- but I get random values. Is it possible to relay the generated PWM on the serial plotter (forgive me if this a dumb question)? (I do not have instrumentation as of now to check it in real time).
2 ) Do you want to create an interrupt of 125 kHz, that will not be reliable. It might work for a short time, but I don't know why that can be helpful for something - Why do you think so? (I am still learning, The comments in program are
3 ) How will this help with reading a signal of 125 kHz after a few µs of the rising edge ? So the PWM generated basically a dummy for a carrier signal(for testing purposes). However, the carrier is generated by an analog module separately which is controlled by an MCU. The carrier is 125kHz and the modulating signal is about 2kHz. I want to read that 2khz signal. So I guess reading the carrier at 4us or less may give me the modulated data. But that's for long term. (If I am making an assumptions, please correct me).
//const byte interrupt_pin = 2;
boolean toggle1 = 0;
int state = 0;
//int measured_periods[SYNC_BITS];
int measured_pwm;
String data;
char a[1000];
void setup() {
// put your setup code here, to run once:
//DDRB &= ~(1<<PB0);//pin 8 input //ICP1
//DDRB &= ~(1<<PB1);
//PORTB |= (1<<PORTB1);//PORTB Input Pullup
//DDRB &= ~(1<<PB
//pinMode(9, OUTPUT);//oc1a
//DDRD &= ~(1<<PD2);//portd pin2 input
//PORTD |= (1<<PD2);
//pinMode(interrupt_pin,INPUT_PULLUP);
//pinMode(dataIn,INPUT);
//pinMode(PB1,OUTPUT);
DDRB |= (1<<PB1); //pin 9 as output
Serial.begin(115200);
Serial.println("AVR Initialization");
delay(50);
//Serial.print(buffer_in);
cli();
//Timer 1 at 62.5kHz
//TCCR1A = 0;
//TCCR1B = 0;
//TCNT1 = 0;
//turn on CTC mode
//TCCR1B |= (1<<ICNC1)|(1<<ICES1)|(1<<WGM12); //CTC MODE - OCR1A as TOP
TCCR1B |= (1<<ICNC1)|(1<<WGM12);
TCCR1A |= (0<<COM1A1)|(1<<COM1A0); //normal 00//toggle 01//low level 10//high level 11
//TCCR1B |= (1<<ICNC1);
OCR1A = 255 ; //[(16000000/(125000*8)] - 1//top value 8 BIT
//Set prescaler 8
//-------> no prescaler - CS10 - 1; 64 prescaler - CS11, CS10 - 1 <-----//
TCCR1B |= (0<<CS12)|(0<<CS11)|(1<<CS10); //no prescaler
//enable timer compare interrupt
//TIMSK1 |= (1<<ICIE1)|(1<<OCIE1A)|(1<<TOIE1);
TIMSK1 |= (1<<TOIE1);
sei();
Serial.println("Finished Setup");
//delay(50);
//allow interrupts
}
//digitalwrite is very slow??
//ISR(TIMER1_COMPA_vect){
//PORTB ^= (1<<0);
// }
/*ISR(TIMER1_CAPT_vect){
//TCNT1 = 0;
edge = TCCR1B;
TCCR1B = edge^(1<<ICES1);
capt = icr - last_capture;
last_capture = icr;
//Serial.println(edge);
//if(capt...
if(TIFR1 &(1<<TOV1)) {
// pending overflow applies if icr< 0x8000
new_time=icr+((uint32_t)(overflows+(icr<0x3F))<<16);
} else {
new_time=icr+((uint32_t)(overflows)<<16);
}
result=new_time-old_time;
old_time=new_time;*/
ISR(TIMER1_OVF_vect){
PORTB ^= (1<<5);
}
void loop() {
// put your main code here, to run repeatedly:
/*
PORTB |= (1<<PORTB5);
delay(8);
PORTB &= ~(1<<PORTB5);
delay(8);*/
//int state = 0;
for(;;){
measured_pwm = pulseIn(PB1,HIGH);
Serial.println(measured_pwm);}
}
short j;
void read_middle(){
for(j=0;j<1000;j++){
a[j] = digitalRead(PB1);
delayMicroseconds(2);
}//Serial.println(a[j]);
}