Hi im trying to get the frequency or PulseWidth using ICP. Im just using a standard square wave of 1ms PulseWidth.
Is there anything wrong with this code as i don't think im getting the right values.
Am i right in assuming that (in my case).
Time = (7.3728M / 8) * PulseWidth
#include#define F_CPU 7372800 #include #include #define BAUDRATE 115200 //calculate UBRR value #define UBRRVAL 3//((F_CPU/(BAUDRATE*16))-1) (9600bps 47ubbr) (115200 3ubbr) void USART_Init() { //Set baud rate UBRR0L=(uint8_t)UBRRVAL; //low byte register UBRR0H=(UBRRVAL>>8); //high byte register //Set data frame format: UCSR0C &=~(1<<UMSEL01)|(1<<UMSEL00); //asynchronous mode UCSR0C &=~(1<<UPM01)|(1<<UPM00); //No parity UCSR0C &=~(1<<USBS0); //1 stop bit UCSR0C |=(1<<UCSZ01)|(1<<UCSZ00); //8 bit size UCSR0B &=~(0<<UCSZ02); //8 bit size UCSR0B |=(1<<RXEN0)|(1<<TXEN0); //Enable Receiver & Transmitter // UCSR0B |=(1<<RXCIEO)|(1<<TXCIEO); } void USART_SendByte(uint8_t u8Data) { while((UCSR0A&(1<<UDRE0)) == 0); // Wait if a byte is being transmitted UDR0 = u8Data; // Transmit data // while((UCSR0A & (1<<TXC0)) == 0); //Must be done. Wait until byte has been transmitted // UCSR0A |= (1<<TXC0); } void ICP_Init(void) { //ICP1 enable TIMSK1 |=(1<<ICIE1); //enable input capture interrupt TCCR1B |=(1<<ICNC1)|(1<<ICES1)|(1<<CS11); //Noise canceller, rising edge, with 8 prescaler TIFR1 |= (1<<ICF1); //clear interrupt-flag } volatile uint16_t PulseWidth; //Kept in memory, not forgotten until re-written ISR(TIMER1_CAPT_vect) { PulseWidth = ICR1L; PulseWidth = (ICR1H<<8); //TCNT1 = 0; } int main(void) { USART_Init(); ICP_Init(); sei(); for(;;) { if((TIFR1 & (1<<ICF1)) == 0) { USART_SendByte(PulseWidth); USART_SendByte(PulseWidth>>8); } } }