Posts

Showing posts from August, 2014

Arduino pure programming part-6 uart loopback test

Connect the TX and RX of the arduino using a wire #include <avr/io.h> #include <avr/interrupt.h> /* UDR-->UART Data Register UCSRA,B,C---> Uart control status register a,b,c UBRRnL,UBRRnH --> UART Baud Rate Register */ ISR(USART_RX_vect) { if(UDR0 == 'C'){ PORTB |=1<<5; } } void uart_init(unsigned int ubrr) { //ubrrn = fosc/16(Baud) -1.for 9600 = 104 //set baud rate UBRR0H = (unsigned char)ubrr>>8; UBRR0L = (unsigned char)ubrr; //enable the receiver and transmitter and receiver enable interrupt UCSR0B = (1<<RXEN0) |(1<<TXEN0) | (1<<RXCIE0); UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); //8-bit //enable global interrupt sei(); //wait for udr to be empty while(!(UCSR0A&&1<<UDRE0)); UDR0 = 'C'; //wait for transmission to complete while(!(UCSR0A&&1<<TXC0)); while(1); } int main() { DDRB |= 1<<5; PORTB &am

arduino pure programming part5 - external interrupt

#include <avr/io.h> #include <avr/interrupt.h> #define SET_BIT(X,Y) X|=1<<Y #define CLR_BIT(X,Y) X&=~(1<<Y) #define IS_BIT_SET(X,Y) X&(1<<Y) #define TOGGLE_BIT(X,Y) X^=(1<<Y) ISR(INT0_vect) { TOGGLE_BIT(PORTB,PB5); } void delaymicroseconds(unsigned long us) { unsigned long temp = us; SET_BIT(TCCR0B,CS01); //Starting the timer with prescaler clock/8 which is 0.125us while(temp != 0) { TCNT0 = 247; while(!IS_BIT_SET(TIFR0,TOV0)); CLR_BIT(TIFR0,TOV0); temp--; } } int main() { SET_BIT(DDRD,2); SET_BIT(PORTD,2); DDRB |= 1<<5; PORTB &= ~(1 << PB5); EIMSK = 1<<INT0; EICRA = 1<<ISC01; sei(); while (1){delaymicroseconds(3000000U);} return 0; }

arduino pure programming part7--printf implementation

#include <avr/io.h> #include <avr/interrupt.h> /* UDR-->UART Data Register UCSRA,B,C---> Uart control status register a,b,c UBRRnL,UBRRnH --> UART Baud Rate Register */ void print_init(unsigned int ubrr) { //ubrrn = fosc/16(Baud) -1.for 9600 = 104 //set baud rate UBRR0H = (unsigned char)ubrr>>8; UBRR0L = (unsigned char)ubrr; //enable the receiver and transmitter and receiver enable interrupt UCSR0B = (1<<RXEN0) |(1<<TXEN0) ; UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); //8-bit } void putch(unsigned char ch) { while(!(UCSR0A&&1<<UDRE0)); UDR0 = ch; while(!(UCSR0A&&TXC0)); } void print(char *string) { int i=0; while(string[i]) { putch(string[i]); i++; } } int main() { print_init(104); //putch('c'); print("Hello World"); return 0; } To see the output use cutecom or minicom with baud rate set to 9600,8data bits , 1 stop bit and no

Arduino Pure C-Programming Part-4 delaymicroseconds

#include<avr/io.h> #define SET_BIT(X,Y) X|=1<<Y #define CLR_BIT(X,Y) X&=~(1<<Y) #define IS_BIT_SET(X,Y) X&(1<<Y) #define TOGGLE_BIT(X,Y) X^=(1<<Y) void delaymicroseconds(unsigned long us) { unsigned long temp = us; SET_BIT(TCCR0B,CS01); //Starting the timer with prescaler clock/8 which is 0.125us while(temp != 0) { TCNT0 = 247; while(!IS_BIT_SET(TIFR0,TOV0)); CLR_BIT(TIFR0,TOV0); temp--; } } int main() { SET_BIT(DDRB,PB5); TOGGLE_BIT(PORTB,PB5); TOGGLE_BIT(PORTB,PB5); while(1) { delaymicroseconds(5000000L); TOGGLE_BIT(PORTB,PB5); } return 0; }

Hello World in assembly for x86

Code : ;author:md.jamal mohiuddin global _start section .text _start: ;print hello world on the screen mov eax,0x4 ;system call number mov ebx,0x1 ;fd =1 mov ecx,message ;buf points to the message mov edx,11 ;len = 11 int 0x80 ;trap interrupt(software defined) ;exit gracefully mov eax,0x1 ;system call number mov ebx,0x3 ;first argument int 0x80 ;trap interrupt(software defined) section .data message : db "Hello World\n" To execute  nasm -f elf32 -o hello.o hello.asm  ld -o hello hello.o ./hello To check the return type: echo $?

Arduino pure C-programming part -3 EEPROM

/* Writing to the AVR EEPROM */ #include <avr/io.h> #include <avr/eeprom.h> #include <avr/interrupt.h> struct accelerometer_data { int x,y,z; }; struct accelerometer_data data = {10,12,32}; uint8_t i=123; int main() { while(!eeprom_is_ready()); //wait for the eeprom to be ready cli(); eeprom_write_block((const void*)&data,(void *)0,sizeof(struct accelerometer_data)); sei(); while(!eeprom_is_ready());  //wait for the eeprom to be ready cli(); eeprom_write_byte((uint8_t *)40,i); sei(); while(1); }