














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An embedded c program example, including the structure, variable types and sizes, constants, operators, and i/o operations. It covers the basics of c programming for controlling leds and reading inputs from switches.
Typology: Slides
1 / 22
This page cannot be seen from the preview
Don't miss anything!
2
4
#include <mega16.h> // the LED 0 on PORTB will be on unsigned char led_status=0xFE; // Timer 1 overflow interrupt service routine interrupt [TIM1_OVF] void timer1_ovf_isr(void) { // Reinitialize Timer 1 value TCNT1H=0xF8; TCNT1L=0xFB; // Place your code here // move the LED led_status<<=1; led_status+=1; if (led_status==0xFF) led_status=0xFE; // turn on the LED PORTB=led_status; } // Declare your global variables here
// Continue on next slide
5
void main(void) { // Declare your local variables here // Input/Output Ports initialization // Port B initialization // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out // State7=1 State6=1 State5=1 State4=1 State3=1 State2=1 State1=1 State0= PORTB=0xFE; // PORTB is port B driver register DDRB=0xFF; // Data Direction Control Register, 1: output; 0: input // other initializations as needed by your program, such as A/D converter // usually all required work for initialization done by CodeVision Wizard // ---------------
// Global enable interrupts #asm("sei") while (1) { // Place your code here }; }
7
Type Size(Bits) Range
bit 1 0,
char 8 -128 to 127
unsigned char 8 0 to 255
int 16 -32768 to 32767
short int 16 -32768 to 32767
unsigned int 16 0 to 65535
signed int 16 -32768 to 32767
long int 32
unsigned long int 32
signed long int 32
float 32 +-1.175e-38 to +- 3.4e
double 32 +-1.175e-38 to +- 3.4e
8
Numerical Constants decimal 1234 binary 0b hexadecimal 0xff octal 0777
Character Constants character representation Equivalent Hex Value
TAB ‘\t’ ‘\x09’ LF (new line) ‘\n’ ‘\x0a’ CR ‘\r’ ‘\x0d’ Backspace ‘\b’ ‘\x08’ -- -- example printf(“c = %d\n”, c) // printf(“c = %d\n\r”, c) //
10
Given an unsigned char y = 0xC operation result x = ~y x = 0x x = y <<3 x = 0x x = y>>4 x = 0x0C x = y&0x3F x = 0x x = y^1 x = 0xC x = y | 0x10 x = 0xD
other examples: unsigned char z z = PINA & 0x06; PORTB = PORTB | 0x60; PORTB = PORTB & 0xfe;
11
Logical operator AND && OR ||
x =5 and y = (x && y) is true, because both are non-zero (x & y) is false, because 00000101 bitwise AND 00000010 equal to zero
(x || y) is true, because either value is non-zero (x | y) is true, b101 bitwise OR b010 is b111 (non-zero)
13
unsigned char i; // temporary variable
DDRA = 0x00; // set PORTA for input DDRB = 0xFF; // set PORTB for output PORTB = 0x00; // turn ON all LEDs initially
while(1){ // Read input from PORTA. // This port will be connected to the 8 switches i = PINA; // Send output to PORTB. // This port will be connected to the 8 LEDs PORTB = i; }
14
Turn on an LED connected to PB PORTB |= 0xF7; // b11110111; PORTB=0x00 initially; Must do the whole port
Turn on an LED connected to PB PORTB.3 = 0 ; // access the bit 3 of port B, turn on the LED
for (delay = 0; delay < 10000; delay++); // declare delay as int somewhere
PORTB.3 = 1; // turn off the LED
16
Beware division:
17
Relational Operators
Is Equal to == Is Not equal to != Less Than < Less Than or Equal to <= Greater than > Greater Than or equal to >= x = 3 and y = (x == y) FALSE (x != y) TRUE (x < y) TRUE (x<=y) TRUE (x>y) FALSE (x >= y) FALSE
19
x = y assign y to x x++ post-increment x ++x pre-increment x x-- post-decrement x --x pre-decrement x
x += y assign (x+y) to x x - = y assign (x-y) to x x = y assign (xy) to x x /= y assign (x/y) to x x %= y assign (x%y) to x
int x=5; int y; y = ++x; /* x == 6, y == 6 */
int x=5; int y; y = x++; /* x == 6, y == 5 */
20
do // mix up the numbers { // while waiting for button release. first ^= seed>>1; // Exclusive ORing in the moving seed second ^= seed>>2; third ^= seed>>3; seed++; // keep rolling over the seed pattern }
while(PINA.0 == 0); // while the button is pressed