/* * main.c * * Created on: 22 нояб. 2018 г. * Author: maxx */ #include #include #include #include #include //sbi, cbi etc.. #include "avr/wdt.h" // WatchDog #include // printf etc.. #include "uart_extd.h" //#include // itoa etc.. #define PRINTF_EN 1 #if PRINTF_EN #define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args) #else #define PRINTF(...) #endif /* * m1284p minimum template, with one button & one led */ //M644P/M1284p Users LEDS: //LED1/PORTC.4- m644p/m1284p maxxir #define led1_conf() DDRC |= (1<getchar() там опрос блокируется пока символ не будет принят (поллинг) через UART1_RX, т.е. неудобно. */ // STDOUT UART0 TX handler static int uart0_putchar(char ch,FILE *stream) { uart_putc(ch); return 0; } // Очищаем буфер приема UART1 RX (иногда нужно) static void uart0_rx_flash(void) { // Считываем все из ring-buffer UART1 RX unsigned int c; do { c = uart_getc(); } while (( c & UART_NO_DATA ) == 0); // Check RX1 none-empty } //***************** UART0: END //***************** ADC: BEGIN #ifndef ADC_DIV //12.5MHz or over use this ADC reference clock #define ADC_DIV (1<> MCU is: %S; CLK is: %luHz\r\n", str_mcu, F_CPU);// MCU Name && FREQ PRINTF(">> Free RAM is: %d bytes\r\n", freeRam()); //Short Blink LED 3 times on startup unsigned char i = 3; while(i--) { led1_high(); _delay_ms(100); led1_low(); _delay_ms(400); wdt_reset(); } unsigned long prev_millis = 0; unsigned long rx_millis = 0; unsigned long uptime = 0; uint16_t CharIn; while(1) { //Here at least every 1sec wdt_reset(); // WDT reset at least every sec if((millis()-prev_millis)>TICK_PER_SEC) { //Here every 1sec wdt_reset(); // WDT reset at least every sec prev_millis = millis(); led1_tgl(); PRINTF("Uptime %lu sec\r\n", uptime++); //PRINTF("ADC5: %d\r\n", adc_read(5)); //!! SW1 pressing action if(!sw1_read())// Check for SW1 pressed every second { // SW1 is pressed //led1_high(); //LED1 ON if(prev_sw1) { //!! Здесь по факту нажатия кнопки (1->0 SW1) //!! Debug only PRINTF("SW1 is pressed\r\nADC0/PA0 is: %u\r\n", adc_read(0)); //PRINTF("SW1 is pressed\r\n"); }//if(prev_sw1) prev_sw1 = 0; // Store SW1 state for next iteration }//if(!sw1_read()) else { // SW1 is unpressed //led1_low(); // LED1 OFF prev_sw1 = 1;// Store SW1 state for next iteration }//if(!sw1_read())else.. }//if((millis()-prev_millis)>TICK_PER_SEC) if((millis()-rx_millis)>0) { // Here every 1 msec, to check UART RX rx_millis = millis(); // GET UART RX Symbol CharIn = uart_getc(); // Check if char exist // Read until data in RX buffer present while (( CharIn & UART_NO_DATA ) == 0) { wdt_reset(); // WDT reset at least every sec //!! Debug only //Read data from UART0 RX ring buffer & send back echo uart_putc(CharIn+1); // GET UART RX Symbol CharIn = uart_getc(); } }//if((millis()-rx_millis)>0) } return 0; } // Timer0 // 1ms IRQ // Used for millis() timing void timer0_init(void) { /* * * For M128 TCCR0 = (1<250kHz:250-=>1kHz) TIMSK |= 1<250kHz:250-=>1kHz) TIMSK0 |= 1<