#include #include #define BV(bit) (1<<(bit)) // Byte Value => converts bit into a byte value. One at bit location. #define cbi(reg, bit) reg &= ~(BV(bit)) // Clears the corresponding bit in register reg #define sbi(reg, bit) reg |= (BV(bit)) // Sets the corresponding bit in register reg #define HEX__(n) 0x##n##UL #define B8__(x) ((x&0x0000000FLU)?1:0) \ +((x&0x000000F0LU)?2:0) \ +((x&0x00000F00LU)?4:0) \ +((x&0x0000F000LU)?8:0) \ +((x&0x000F0000LU)?16:0) \ +((x&0x00F00000LU)?32:0) \ +((x&0x0F000000LU)?64:0) \ +((x&0xF0000000LU)?128:0) #define B8(d) ((unsigned char)B8__(HEX__(d))) #ifndef F_CPU #define F_CPU 8000000 // Mhz #endif #define UART_BAUD_RATE 9600 // 9600 baud void uart0_init(void); int uart_putchar(char c, FILE *stream); int ADConvert(short Channel, short DivFactor); void adc_init(void); int uart_putchar(char c, FILE *stream) { if (c == '\n') uart_putchar('\r', stream); loop_until_bit_is_set(UCSR0A, UDRE); UDR0 = c; return 0; } void uart0_init(void) { unsigned char baudrateDiv; baudrateDiv = (unsigned char)((F_CPU+(UART_BAUD_RATE*8L))/(UART_BAUD_RATE*16L)-1); UBRR0H = baudrateDiv >> 8; UBRR0L = baudrateDiv; UCSR0B = (1 << RXEN0) | (1 << TXEN0); UCSR0C = (1 << USBS0) | (3 << UCSZ0); fdevopen(uart_putchar, NULL); printf("\n\nuart0_init();\n"); } void enable_external_ram (void) __attribute__ ((naked)) __attribute__ ((section (".init1"))); void enable_external_ram(void) { /* * enable external memory, no wait states */ MCUCR |= _BV(SRE); } int main(void) { unsigned char * xram_ptr; unsigned int i,j,x; uart0_init(); adc_init(); TCCR3A = 0x00; while (1) { // printf("Hello world!"); // read from ADC and output to serial - better serial as library but for now... // toggle bank -- PE7/6/5 DDRE=0xff; PORTE = 0x7F; for (i=0;i<254;i++){ xram_ptr = (char *)(0xFF00+i); *xram_ptr = i; } PORTE = 0x7F; xram_ptr = (char *)0xFF00; for (i=0;i<254;i++){ x = *(xram_ptr+i); printf("%d %d\n", i, x); } // printf("%d\n", ADConvert(0,2)); } } void adc_init(void) { /* sbi(ADCSRA,ADEN); // enables ADC by setting bit 7 (ADEN) in the ADCSRA sbi(ADCSRA,ADFR); // single sample conversion by clearing bit 5 (ADFR) in the ADCSRA ADCSRA = ((ADCSRA & B8(11111000)) | B8(00000111)); // selects div by 64 clock prescaler ADMUX = ((ADMUX & B8(00111111)) | B8(01000000)); // selects AVCC as Vref cbi(ADMUX,ADLAR); // selects right adjust of ADC result ADMUX &= B8(11100000); // selects single-ended conversion on PF0*/ ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Set ADC prescalar to 128 - 125KHz sample rate @ 16MHz ADMUX |= (1 << REFS0); // Set ADC reference to AVCC // ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading // No MUX values needed to be changed to use ADC0 ADCSRA |= (1 << ADFR); // Set ADC to Free-Running Mode ADCSRA |= (1 << ADEN); // Enable ADC DDRF = 0x00; // configure a2d port (PORTF) as input so we can receive analog signals PORTF = 0x00; // make sure pull-up resistors are turned off (else we’ll just read 0xCFF) } int ADConvert(short Channel, short DivFactor) { int ADresult;/* Result to be returned*/ sbi(ADCSRA, ADSC); /* Start ADC*/ loop_until_bit_is_set(ADCSRA, ADIF); /* Wait for ADIF, will happen soon */ /* * Read the input value */ ADresult = ADCL; /* Read LSB first */ ADresult |= ((int)ADCH) << 8;/* then MSB */ /* * Clear the ADCSRA, including the AD Interrupt Flag (by setting it!) */ // ADCSRA = 0x10; return(ADresult); }