Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* main.c
- * Created on: 17 lis 2019
- * Author: Student */
- //1.jedna dioda
- /*
- #include <avr/io.h>
- #include <util/delay.h>
- #define LED_PIN ( 1 << PB0 )
- #define LED_TOG PORTB ^= LED_PIN //zmiana stanu
- int main (void) {
- DDRB |= LED_PIN; //ustawienie portu jako wyjsciowy
- while(1) {
- LED_TOG;
- _delay_ms(1000);
- }
- }
- */
- //2.dwie diody
- /*
- #include <avr/io.h>
- #include <util/delay.h>
- #define LED_PIN1 ( 1 << PB0 )
- #define LED_ON1 PORTB &= ~LED_PIN1 //włączenie
- #define LED_OFF1 PORTB |= LED_PIN1 //wyłączenie
- #define LED_TOG1 PORTB ^= LED_PIN1 //zmiana stanu
- #define LED_PIN2 ( 1 << PB1 )
- #define LED_ON2 PORTB &= ~LED_PIN2 //włączenie
- #define LED_OFF2 PORTB |= LED_PIN2 //wyłączenie
- #define LED_TOG2 PORTB ^= LED_PIN2 //zmiana stanu
- int main (void) {
- DDRB |= LED_PIN1;
- DDRB |= LED_PIN2;
- while(1) {
- LED_OFF2;
- LED_ON1;
- _delay_ms(1000);
- LED_OFF1;
- LED_ON2;
- _delay_ms(1000);
- }
- }
- */
- //3.Diody zapalają się od lewej do prawej
- /*
- #include <avr/io.h>
- #include <util/delay.h>
- int main (void) {
- DDRB = 0xff; //do calego rejestru ustawiamy 1
- PORTB = 0xff; //dla calego portu ustawiamy 1 czyli gasimy diody bo diody podlaczone katoda
- while(1) {
- for(uint8_t i=0; i<8; i++) {
- PORTB =~ ( 1 << i);
- _delay_ms(200);
- }
- }
- }
- */
- //4.Diody zapalają się od lewej do prawej i potem od prawej do lewej
- /*
- #include <avr/io.h>
- #include <util/delay.h>
- int main(void) {
- DDRB = 0xff;
- PORTB = 0xff;
- while(1) {
- for(uint8_t i=0; i<8; i++) {
- PORTB =~ ( 1 << i);
- _delay_ms(200);
- }
- for(uint8_t i=6; i>0; i--) {
- PORTB =~ ( 1 << i);
- _delay_ms(200);
- }
- }
- }
- */
- //5.Diody gasna od prawej do lewej i potem od lewej do prawej
- /*
- #include <avr/io.h>
- #include <util/delay.h>
- int main(void) {
- DDRB = 0xff;
- PORTB = 0xff;
- while(1) {
- for(uint8_t i=0; i<8; i++) {
- PORTB = ( 1 << i);
- _delay_ms(200);
- }
- for(uint8_t i=6; i>0; i--) {
- PORTB = ( 1 << i);
- _delay_ms(200);
- }
- }
- }
- */
- //6. Dioda zapalana z przycisku
- /*
- #include <avr/io.h>
- #include <util/delay.h>
- #define LED_PIN ( 1 << PB0 )
- #define LED_TOG PORTB ^=LED_PIN
- #define LED_OFF PORTB |=LED_PIN
- #define KEY_PIN (1<<PC5)
- #define KEY_DOWN ! (PINC & KEY_PIN)
- int main(void){
- DDRB |= LED_PIN;
- PORTB |= LED_PIN;
- DDRC &= ~KEY_PIN;
- PORTC |= KEY_PIN;
- while(1)
- {
- if(KEY_DOWN){
- _delay_ms(80);
- if(KEY_DOWN){
- LED_TOG;
- _delay_ms(500);
- }
- }else LED_OFF;
- }
- }*/
- // 7.Przycisk i dioda ale inaczej
- /*
- #include <avr/io.h>
- #include <util/delay.h>
- #define LED_PIN ( 1 << PB0 )
- #define LED_TOG PORTB ^=LED_PIN
- #define LED_OFF PORTB |=LED_PIN
- #define KEY_PIN (1<<PC5)
- #define KEY_DOWN ! (PINC & KEY_PIN)
- uint8_t Button_On(void);
- int main(void) {
- DDRB |= LED_PIN;
- PORTB |= LED_PIN;
- DDRC &= ~KEY_PIN;
- PORTC |= KEY_PIN;
- while(1) {
- if(Button_On()) {
- LED_TOG;
- _delay_ms(500);
- } else LED_OFF;
- }
- }
- uint8_t Button_On(void) {
- if(KEY_DOWN) {
- _delay_ms(80);
- if(KEY_DOWN) { return 1; }
- }
- return 0;
- }
- */
- //8.Jeden klawisz wlacza miganie diody, drugi wylacza. Oba wcisniete jednoczesnie
- /*
- #include <avr/io.h>
- #include <util/delay.h>
- #define LED_PIN (1<<PB0)
- #define LED_TOG PORTB ^= LED_PIN
- #define LED_OFF PORTB |= LED_PIN //wyłączenie
- #define KEY_PIN (1<<PC5)
- #define KEY_DOWN ! (PINC & KEY_PIN)
- #define KEY_PIN1 (1<<PC4)
- #define KEY_DOWN1 ! (PINC & KEY_PIN1)
- uint8_t Button_On(void);
- uint8_t Button_Off(void);
- void mk_delay_ms( uint16_t ms ) {
- while(ms--) { _delay_ms(1); }
- }
- int main(void) {
- DDRB |= LED_PIN;
- PORTB |= LED_PIN;
- DDRC &= ~KEY_PIN;
- PORTC |= KEY_PIN;
- DDRC &= ~KEY_PIN1;
- PORTC |= KEY_PIN1;
- while(1) {
- if( Button_On() == 1 && Button_Off() == 0 ) {
- LED_TOG;
- mk_delay_ms(500);
- }
- if( Button_On() == 1 && Button_Off() == 1 ) { LED_OFF; }
- if( Button_On() == 0 && Button_Off() == 0 ) { LED_OFF; }
- }
- }
- uint8_t Button_On(void) {
- if(KEY_DOWN) {
- mk_delay_ms(80);
- if(KEY_DOWN) return 1;
- }
- return 0;
- }
- uint8_t Button_Off(void) {
- if(KEY_DOWN1) {
- mk_delay_ms(80);
- if(KEY_DOWN1) return 1;
- }
- return 0;
- }
- */
- //9.Cztery klawisze wszystkie diody
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #define KEY_PIN (1<<PC5) //Def. pinu do ktorego podlaczony przycisk
- #define KEY_DOWN ! (PINC & KEY_PIN) //makro sprawdzajace czy jest stan 0
- #define KEY_PIN1 (1<<PC4) //Def. pinu do ktorego podlaczony przycisk
- #define KEY_DOWN1 ! (PINC & KEY_PIN1) //makro sprawdzajace czy jest stan 0
- #define KEY_PIN2 (1<<PC6) //Def. pinu do ktorego podlaczony przycisk
- #define KEY_DOWN2 ! (PINC & KEY_PIN2) //makro sprawdzajace czy jest stan 0
- void mk_delay_ms( uint16_t ms ) {
- while(ms--) { _delay_ms(1); }
- }
- /*void mk_delay_ms( uint16_t ms ) {
- while(ms--) { _delay_ms(1); }
- }*/
- volatile uint8_t licznik; //zmienna do do zliczania taktow
- uint8_t Lewo_Prawo(void), Flag_Lewo_Prawo = 0, StanBefore = 0, StanNow = 0;
- uint8_t Prawo_Lewo(void), Flag_Prawo_Lewo = 0, StanBefore1 = 0, StanNow1 = 0;
- uint8_t Wygas(void), Flag_Wygas = 0, StanBefore2 = 0, StanNow2 = 0;
- int main(void) {
- DDRB = 0xff; //do calego rejestru D ustawiamy 1
- PORTB = 0xff; //dla calego portu D ustawiamy 1 czyli gasimy diody bo diody podlaczone katoda
- DDRC &= ~KEY_PIN; //kierunek pinu jako wejsciowy
- PORTC |= KEY_PIN; //podciagniecie pinu do VCC (wew. rezystor)
- DDRC &= ~KEY_PIN1; //kierunek pinu jako wejsciowy
- PORTC |= KEY_PIN1; //podciagniecie pinu do VCC (wew. rezystor)
- DDRC &= ~KEY_PIN2; //kierunek pinu jako wejsciowy
- PORTC |= KEY_PIN2; //podciagniecie pinu do VCC (wew. rezystor)
- //timer///////////////////////////////////////////////////
- //TCCR0 |= (1<<CS02); //preskaler = 256 czyli 1 takt przy 8MHz 31250 timer zlicza impulsy z 8MHz/256= 31250 Hz czyli 0,032 ms
- TCCR0 |= (1<<CS02) | (1<<CS00); //1024 czyli 8Mhz/1024= 7812,5 Hz czyli 0,128 ms
- TIMSK |= (1<<TOIE0); //Zezwolenie na przerwanie Compare Match
- sei();
- //timer///////////////////////////////////////////////////
- while(1){
- StanNow = KEY_DOWN; //obsl. przycisk
- if(StanNow != StanBefore) { //wykr. zmiany stanu
- mk_delay_ms(80);
- if(StanNow != StanBefore) {
- if(StanNow == 0) { //zb opadaj. przycisk wcisniety
- Flag_Lewo_Prawo = !Flag_Lewo_Prawo;
- Flag_Prawo_Lewo = 0;
- }
- StanBefore = StanNow;
- }
- }
- if(Flag_Lewo_Prawo == 1) { Lewo_Prawo(); }
- StanNow1 = KEY_DOWN1; //obsl. przycisk
- if(StanNow1 != StanBefore1) { //wykr. zmiany stanu
- mk_delay_ms(80);
- if(StanNow1 != StanBefore1) {
- if(StanNow1 == 0) { //zb opadaj. przycisk wcisniety
- Flag_Prawo_Lewo = !Flag_Prawo_Lewo;
- Flag_Lewo_Prawo = 0;
- }
- StanBefore1 = StanNow1;
- }
- }
- if(Flag_Prawo_Lewo == 1) { Prawo_Lewo(); }
- StanNow2 = KEY_DOWN2; //obsl. przycisk
- if(StanNow2 != StanBefore2) { //wykr. zmiany stanu
- mk_delay_ms(80);
- if(StanNow2 != StanBefore2) {
- if(StanNow1 == 0) { //zb opadaj. przycisk wcisniety
- Flag_Wygas = !Flag_Wygas;
- Flag_Wygas = 0;
- }
- StanBefore2 = StanNow2;
- }
- }
- if(Flag_Wygas == 1) { Wygas(); }
- if(KEY_DOWN2) {
- mk_delay_ms(80);
- if(KEY_DOWN2) PORTB = 0xFF;
- }
- }
- }
- ISR(TIMER0_OVF_vect) { ++licznik; }
- uint8_t Lewo_Prawo(void) {
- static volatile uint8_t step = 0;
- if ( licznik >= 10){
- if(++step == 8) step = 0;
- PORTB = ~(1<<(7-step));
- licznik=0;
- }
- }
- uint8_t Prawo_Lewo(void) {
- static volatile uint8_t step = 0;
- if ( licznik >= 10){
- if(++step == 8) step = 0;
- PORTB = ~(1<<step);
- licznik=0;
- }
- }
- /*uint8_t Wygas(void) {
- static volatile uint8_t step = 0;
- if ( licznik >= 10){
- ku+as=10;
- licznik=0;
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement