Skip to content

Instantly share code, notes, and snippets.

@theapi
Created January 20, 2014 13:20
Show Gist options
  • Save theapi/8519735 to your computer and use it in GitHub Desktop.
Save theapi/8519735 to your computer and use it in GitHub Desktop.
Port manipulation in avr-c
/*
* main.c
*
* Created on: 19 Jan 2014
* Author: peter
*/
//#define F_CPU 16000000L
#include <avr/io.h>
#include <util/delay.h>
void iterateLeds()
{
for (int8_t x = 0; x<8; x++) {
PORTD = (0x01 << x);
_delay_ms(100);
}
}
void cylon()
{
uint16_t delay = 80;
for (int8_t x = 0; x <= 7; x++) {
PORTD = (0x01 << x);
_delay_ms(delay);
}
for (int8_t x = 7; x >= 0; x--) {
PORTD = (0x01 << x);
_delay_ms(delay);
}
}
void ledCountTime() {
for (int8_t x = 0; x <= 255; x++) {
PORTD = x;
_delay_ms(1000);
}
}
void ledLinePulse()
{
uint16_t delay = 100;
for (int8_t x = 0; x <= 7; x++) {
PORTD |= (0x01 << x);
_delay_ms(delay);
}
for (int8_t x = 7; x >= 0; x--) {
PORTD &= ~(0x01 << x);
_delay_ms(delay);
}
}
void ledPulse()
{
uint16_t delay = 100;
int8_t i = 0; // the led to turn off
for (int8_t x = 0; x <= 7; x++) {
i = x - 3;
if (x < 3) {
i = x + 5;
}
PORTD &= ~(0x01 << (i)); // turn off an led
PORTD |= (0x01 << x); // turn on an led
_delay_ms(delay);
}
}
int main(void) {
/*
DDRD |= 1<<PD7; // set to output
while(1) {
PORTD &= ~(1<<PD7); // LED on
_delay_ms(250);
PORTD |= 1<<PD7; // LED off
_delay_ms(250);
}
return 0;
*/
DDRD = 0xFF; // set all to output
PORTD = 0; // all off
while(1) {
cylon();
//ledCountTime();
//ledLinePulse();
//ledPulse();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment