opdracht 2.1
This commit is contained in:
86
Microcontrollers/opdracht 2.1/main.c
Normal file
86
Microcontrollers/opdracht 2.1/main.c
Normal file
@@ -0,0 +1,86 @@
|
||||
/* ---------------------------------------------------------------------------
|
||||
** This software is in the public domain, furnished "as is", without technical
|
||||
** support, and with no warranty, express or implied, as to its usefulness for
|
||||
** any purpose.
|
||||
**
|
||||
** ioisr.c
|
||||
**
|
||||
** Beschrijving: ISR on PORTD demonstrattion
|
||||
** Target: AVR mcu
|
||||
** Build: avr-gcc -std=c99 -Wall -O3 -mmcu=atmega128 -D F_CPU=8000000UL -c ioisr.c
|
||||
** avr-gcc -g -mmcu=atmega128 -o ioisr.elf ioisr.o
|
||||
** avr-objcopy -O ihex ioisr.elf ioisr.hex
|
||||
** or type 'make'
|
||||
** Author: dkroeske@gmail.com
|
||||
** -------------------------------------------------------------------------*/
|
||||
|
||||
#define F_CPU 8e6
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
|
||||
/******************************************************************
|
||||
/*
|
||||
short: Busy wait number of millisecs
|
||||
inputs: int ms (Number of millisecs to busy wait)
|
||||
outputs:
|
||||
notes: Busy wait, not very accurate. Make sure (external)
|
||||
clock value is set. This is used by _delay_ms inside
|
||||
util/delay.h
|
||||
Version : DMK, Initial code
|
||||
*******************************************************************/
|
||||
void wait( int ms ) {
|
||||
for (int i=0; i<ms; i++) {
|
||||
_delay_ms( 1 ); // library function (max 30 ms at 8MHz)
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************************
|
||||
short: ISR INT0
|
||||
inputs:
|
||||
outputs:
|
||||
notes: Set PORTD.5
|
||||
Version : DMK, Initial code
|
||||
*******************************************************************/
|
||||
ISR( INT0_vect ) {
|
||||
PORTD |= (1<<5);
|
||||
}
|
||||
|
||||
/******************************************************************
|
||||
short: ISR INT1
|
||||
inputs:
|
||||
outputs:
|
||||
notes: Clear PORTD.5
|
||||
Version : DMK, Initial code
|
||||
*******************************************************************/
|
||||
ISR( INT1_vect ) {
|
||||
PORTD &= ~(1<<5);
|
||||
}
|
||||
|
||||
/******************************************************************
|
||||
short: main() loop, entry point of executable
|
||||
inputs:
|
||||
outputs:
|
||||
notes: Slow background task after init ISR
|
||||
Version : DMK, Initial code
|
||||
*******************************************************************/
|
||||
int main( void ) {
|
||||
// Init I/O
|
||||
DDRD = 0xF0; // PORTD(7:4) output, PORTD(3:0) input
|
||||
|
||||
// Init Interrupt hardware
|
||||
EICRA |= 0x0B; // INT1 falling edge, INT0 rising edge
|
||||
EIMSK |= 0x03; // Enable INT1 & INT0
|
||||
|
||||
// Enable global interrupt system
|
||||
//SREG = 0x80; // Of direct via SREG of via wrapper
|
||||
sei();
|
||||
|
||||
while (1) {
|
||||
PORTD ^= (1<<7); // Toggle PORTD.7
|
||||
wait( 500 );
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user