Changeset 82d2b68 in avrstuff for chiptest/main.c


Ignore:
Timestamp:
Jul 29, 2014, 9:17:17 PM (10 years ago)
Author:
Adrien Destugues <pulkomandy@…>
Branches:
main
Children:
efe25a8
Parents:
5ef7bfc
Message:

Make baudrate computation aware of F_CPU.

git-svn-id: svn://pulkomandy.tk/avrstuff@107 c6672c3c-f6b6-47f9-9001-1fd6b12fecbe

File:
1 edited

Legend:

Unmodified
Added
Removed
  • chiptest/main.c

    r5ef7bfc r82d2b68  
    1414#define LEDBIT 1 << PB2
    1515
     16#define BAUD 9600 // Safe value even for low clocks. (used by setbaud.h)
     17
    1618int main() {
    1719        wdt_enable(WDTO_2S);
    18     // configure timer 0 for a rate of 16M/(256 * 256) = ~244Hz
     20    // configure timer 0 for a rate of FCPU/(256 * 256)
    1921    TCCR0A = 0;          // timer 0 prescaler: 256
    2022        TCCR0B = 4;
     
    2527        PORTLED = 0;
    2628
    27         // Serial PORT
    28         UBRRH = 0;
    29         UBRRL = 10;
     29        // Serial baudrate - use avrlibc magic to compute the baudrate register
     30        // values.
     31        #include <util/setbaud.h>
     32        UBRRH = UBRRH_VALUE;
     33        UBRRL = UBRRL_VALUE;
     34        #if USE_2X
     35                UCSRA |= (1 << U2X);
     36        #else
     37                UCSRA &= ~(1 << U2X);
     38        #endif
    3039
     40        // Enable the serial port.
    3141        UCSRB = (1<<RXEN) |(1<<TXEN);
    3242        UCSRC = (1 << UCSZ1) | (1 << UCSZ0);
    3343
    34         while(1) {
     44        // Let's rock!
     45        uint8_t counter = 0;
     46        for(;;) {
    3547                wdt_reset();
    3648
    37 
    38                 // check timer if we need periodic reports
     49                // Wait for timer overflow...
    3950                if (TIFR & (1 << TOV0)) {
    4051                        TIFR = (1 << TOV0); // reset flag
    41                         PORTLED ^= LEDBIT;
     52                        counter++;
     53                }
    4254
    43                         UDR = 'H';
     55                if (counter == 0)
     56                {
     57                        PORTLED ^= LEDBIT; // Toggle the LED
     58                        UDR = 'H'; // Send a byte to the UART
    4459                }
    4560        }
Note: See TracChangeset for help on using the changeset viewer.