Interfacing AT Tiny 13 for Controlling PWM with ADC input

 

/*****************************************************

This program was produced by the
CodeWizardAVR V2.03.9 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.

http://www.hpinfotech.com

Project :
Version :
Date : 11/23/2011
Author : Triwiyanto
Company : Tekmed
Comments:
Chip type : ATtiny13
AVR Core Clock frequency: 1.000000 MHz
Memory model : Tiny
External RAM size : 0
Data Stack size : 16
*****************************************************/

#include <tiny13.h>

#include <delay.h>

#define ADC_VREF_TYPE 0×60

// Read the 8 most significant bits
// of the AD conversion result
unsigned char read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0×40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0×10)==0);
ADCSRA|=0×10;
return ADCH;
}

// Declare your global variables here
unsigned char data;
void main(void)
{
// Declare your local variables here

// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0×80;
CLKPR=0×00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

// Input/Output Ports initialization
// Port B initialization
// Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out
// State5=T State4=T State3=T State2=T State1=T State0=0
PORTB=0×00;
DDRB=0×01;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 3.906 kHz
// Mode: Phase correct PWM top=FFh
// OC0A output: Non-Inverted PWM
// OC0B output: Disconnected
TCCR0A=0×81;
TCCR0B=0×04;
TCNT0=0×00;
OCR0A=0×00;
OCR0B=0×00;

// External Interrupt(s) initialization
// INT0: Off
// Interrupt on any change on pins PCINT0-5: Off
GIMSK=0×00;
MCUCR=0×00;

// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0×00;

// Analog Comparator initialization
// Analog Comparator: Off
ACSR=0×80;
ADCSRB=0×00;

// ADC initialization
// ADC Clock frequency: 15.625 kHz
// ADC Bandgap Voltage Reference: On
// ADC Auto Trigger Source: Free Running
// Only the 8 most significant bits of
// the AD conversion result are used
// Digital input buffers on ADC0: On, ADC1: On, ADC2: On, ADC3: On
DIDR0&=0×03;
DIDR0|=0×00;
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0xA6;
ADCSRB&=0xF8;

while (1)
{
// Place your code here
data=read_adc(1);
OCR0A=data;
};
}

Posted in Tutorial AVR ATTiny | Leave a comment

Experiment PWM with AT Tiny 13

1. Experiment PWM with auto varies duty cycle

/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.9 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.

http://www.hpinfotech.com

Project :
Version :
Date : 11/18/2011
Author : Triwiyanto
Company : Tekmed
Comments:
Chip type : ATtiny13
AVR Core Clock frequency: 1.000000 MHz
Memory model : Tiny
External RAM size : 0
Data Stack size : 16
*****************************************************/

#include <tiny13.h>
#include <delay.h>

// Timer 0 output compare A interrupt service routine
interrupt [TIM0_COMPA] void timer0_compa_isr(void)
{
// Place your code here

}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0×80;
CLKPR=0×00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

// Input/Output Ports initialization
// Port B initialization
// Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=Out
// State5=T State4=T State3=T State2=T State1=0 State0=0
PORTB=0×03;
DDRB=0×03;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 125.000 kHz
// Mode: Fast PWM top=FFh
// OC0A output: Non-Inverted PWM
// OC0B output: Disconnected
// (Set OC0A on Compare Match, clear OC0A at TOP)
// WGM01 – WGM00 (set fast PWM)
TCCR0A=0b11000011;
TCCR0B=0×05;
TCNT0=0×00;
OCR0A=0×00;
OCR0B=0×00;
// External Interrupt(s) initialization
// INT0: Off
// Interrupt on any change on pins PCINT0-5: Off
GIMSK=0×00;
MCUCR=0×00;

// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0×04;

// Analog Comparator initialization
// Analog Comparator: Off
ACSR=0×80;
ADCSRB=0×00;

// Global enable interrupts
#asm(“sei”)

while (1)
{
unsigned char i;
// Place your code here
for (i=0;i<255;i++)
{
OCR0A=i;
delay_ms(1000);
};
}
}

2. Experiment PWM with setting Duty Cycle

/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.9 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.

http://www.hpinfotech.com

Project :
Version :
Date : 11/18/2011
Author : Triwiyanto
Company : Tekmed
Comments:
Chip type : ATtiny13
AVR Core Clock frequency: 1.000000 MHz
Memory model : Tiny
External RAM size : 0
Data Stack size : 16
*****************************************************/

#include <tiny13.h>
#include <delay.h>

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0×80;
CLKPR=0×00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

// Input/Output Ports initialization
// Port B initialization
// Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out
// State5=T State4=T State3=T State2=T State1=T State0=0
DDRB=0b00000001;
PORTB=0b11111111;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 125.000 kHz
// Mode: Normal top=FFh
// OC0A output: Toggle on compare match
// OC0B output: Disconnected
TCCR0A=0b11000011;
TCCR0B=0×05;
TCNT0=0×00;
OCR0A=0×00;
OCR0B=0×00;

// External Interrupt(s) initialization
// INT0: Off
// Interrupt on any change on pins PCINT0-5: Off
GIMSK=0×00;
MCUCR=0×00;

// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0×00;

// Analog Comparator initialization
// Analog Comparator: Off
ACSR=0×80;
ADCSRB=0×00;
OCR0A=50;//Output to PORTB.0
while (1)
{
// Place your code here
if (PINB.3==0)
{OCR0A++;
delay_ms(100);
}
else if (PINB.4==0)
{OCR0A–;
delay_ms(100);
}
}
}

 

 

Posted in Tutorial AVR ATTiny | Leave a comment

Experiment counter in AT Tiny 13

1. Experiment counter using T0 and TCNT0 register


 /*****************************************************

This program was produced by the
CodeWizardAVR V2.03.9 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.

http://www.hpinfotech.com

Project :
Version :
Date : 11/18/2011
Author : Triwiyanto
Company : Tekmed
Comments:
Chip type : ATtiny13
AVR Core Clock frequency: 1.000000 MHz
Memory model : Tiny
External RAM size : 0
Data Stack size : 16
*****************************************************/

#include <tiny13.h>

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0×80;
CLKPR=0×00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

// Input/Output Ports initialization
// Port B initialization
// Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State5=T State4=T State3=T State2=T State1=T State0=T
DDRB=0×03;
PORTB=0×03;
// Timer/Counter 0 initialization
// Clock source: T0 pin Falling Edge
// Mode: Normal top=FFh
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0×00;
TCCR0B=0×06;
TCNT0=0×00;
OCR0A=0×00;
OCR0B=0×00;

// External Interrupt(s) initialization
// INT0: Off
// Interrupt on any change on pins PCINT0-5: Off
GIMSK=0×00;
MCUCR=0×00;

// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0×00;

// Analog Comparator initialization
// Analog Comparator: Off
ACSR=0×80;
ADCSRB=0×00;

while (1)
{
if (TCNT0<20)
{PORTB.0=0;}
else if (TCNT0>20)
{PORTB.0=1;
PORTB.1=0;}
// Place your code here

};
}

2. Experiment counter using Interupt Service Routine

/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.9 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.

http://www.hpinfotech.com

Project :
Version :
Date : 11/19/2011
Author : Triwiyanto
Company : Tekmed
Comments:
Chip type : ATtiny13
AVR Core Clock frequency: 1.000000 MHz
Memory model : Tiny
External RAM size : 0
Data Stack size : 16
*****************************************************/

#include <tiny13.h>
#include <delay.h>
unsigned int i;
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here
i++;
if (i==10000)
{i=0;
PORTB.0=0;
delay_ms(100);
}
else
{PORTB.0=1;
}
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0×80;
CLKPR=0×00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

// Input/Output Ports initialization
// Port B initialization
// Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0b11111111;
DDRB=0b11111111;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 125.000 kHz
// Mode: Normal top=FFh
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0×00;
TCCR0B=0×02;
TCNT0=0×00;
OCR0A=0×00;
OCR0B=0×00;

// External Interrupt(s) initialization
// INT0: Off
// Interrupt on any change on pins PCINT0-5: Off
GIMSK=0×00;
MCUCR=0×00;

// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0×02;

// Analog Comparator initialization
// Analog Comparator: Off
ACSR=0×80;
ADCSRB=0×00;

// Global enable interrupts
#asm(“sei”)
i=0;
while (1)
{
// Place your code here

};
}

Posted in Tutorial AVR ATTiny | Leave a comment

Experiment Comparator AT tiny 13

#include <tiny13.h>
#include <delay.h>

// Analog Comparator interrupt service routine
interrupt [ANA_COMP] void ana_comp_isr(void)
{
// Place your code here
PORTB.2=0;
delay_ms(1000);
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0×80;
CLKPR=0×00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

PORTB=0b11111111;
DDRB=0b00000100;
// Analog Comparator initialization
// Analog Comparator: On
// Digital input buffers on AIN0: On, AIN1: On
DIDR0=0×00;
// Interrupt on Output Toggle
ACSR=0×08;
ADCSRB=0×00;

// Global enable interrupts
#asm(“sei”)

while (1)
{
// Place your code here
PORTB.2=1;
};
}

 

 

 

Posted in Tutorial AVR ATTiny | Leave a comment

Experiment Internal ADC AT Tiny13

 

/*****************************************************
Project :
Date : 11/18/2011
Author : Triwiyanto
Company : Tekmed
Comments:
Chip type : ATtiny13
AVR Core Clock frequency: 1.000000 MHz
Memory model : Tiny
External RAM size : 0
Data Stack size : 16
*****************************************************/

#include <tiny13.h>
#include <delay.h>
#define ADC_VREF_TYPE 0×40

// ADC interrupt service routine
interrupt [ADC_INT] void adc_isr(void)
{
unsigned int adc_data;
// Read the AD conversion result
adc_data=ADCW;
// Place your code here
if (adc_data > 100)
{
PORTB.0=0;
}
else if (adc_data < 50)
{
PORTB.0=1;
}
else
{
PORTB.0=0;
}
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here
// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0×80;
CLKPR=0×00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

// Input/Output Ports initialization
// Port B initialization
// Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out
// State5=T State4=T State3=T State2=T State1=T State0=0
PORTB=0×00;
DDRB=0×01;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0×00;
TCCR0B=0×00;
TCNT0=0×00;
OCR0A=0×00;
OCR0B=0×00;

// External Interrupt(s) initialization
// INT0: Off
// Interrupt on any change on pins PCINT0-5: Off
GIMSK=0×00;
MCUCR=0×00;

// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0×00;

// Analog Comparator initialization
// Analog Comparator: Off
ACSR=0×80;
ADCSRB=0×00;

// ADC initialization
// ADC Clock frequency: 125.000 kHz
// ADC Bandgap Voltage Reference: On
// ADC Auto Trigger Source: Free Running
// Digital input buffers on ADC0: On, ADC1: On, ADC2: On, ADC3: On
DIDR0&=0×03;
DIDR0|=0×00;
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0xAB;
ADCSRB&=0xF8;

// Global enable interrupts
#asm(“sei”)

while (1)
{
// Place your code here

};
}

Posted in Tutorial AVR ATTiny | Leave a comment

Experiment AT Tiny 13 with switch

 

/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.9 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.

http://www.hpinfotech.com

Project :
Version :
Date : 11/19/2011
Author : Triwiyanto
Company : Tekmed
Comments:
Chip type : ATtiny13
AVR Core Clock frequency: 1.000000 MHz
Memory model : Tiny
External RAM size : 0
Data Stack size : 16
*****************************************************/

#include <tiny13.h>
#include <delay.h>

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0×80;
CLKPR=0×00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

// Input/Output Ports initialization
// Port B initialization
// Func5=In Func4=Out Func3=Out Func2=Out Func1=In Func0=In
// State5=T State4=0 State3=0 State2=0 State1=P State0=P
DDRB=0b00011100;
PORTB=0b00000011;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0×00;
TCCR0B=0×00;
TCNT0=0×00;
OCR0A=0×00;
OCR0B=0×00;

// External Interrupt(s) initialization
// INT0: Off
// Interrupt on any change on pins PCINT0-5: Off
GIMSK=0×00;
MCUCR=0×00;

// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0×00;

// Analog Comparator initialization
// Analog Comparator: Off
ACSR=0×80;
ADCSRB=0×00;
PORTB.2=1;
PORTB.3=1;
while (1)
{
if (PINB.0==0)
{
PORTB.2=0;
PORTB.3=1;
PORTB.4=1;
}
else if (PINB.1==0)
{
PORTB.2=1;
PORTB.3=0;
PORTB.4=1;
}
else
{
PORTB.2=1;
PORTB.3=1;
PORTB.4=1;
delay_ms(500);
PORTB.2=0;
PORTB.3=0;
PORTB.4=0;
delay_ms(500);
}
// Place your code here
};
}

 

Posted in Tutorial AVR ATTiny | Leave a comment

Varies PWM from minimum to maximum on OCR1A

In this experiment microcontroller will output PWM with vary duty cycle min to max on OCR1A

#include <mega32.h>

#include <delay.h>
// Declare your global variables here
void main(void)
{
// Declare your local variables here

int CW_var;

// Input/Output Ports initialization
// Port A initialization
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T
PORTA=0×00;
DDRA=0×00;

// Port B initialization
// Func0=In Func1=In Func2=In Func3=Out Func4=In Func5=In Func6=In Func7=In
// State0=T State1=T State2=T State3=0 State4=T State5=T State6=T State7=T
PORTB=0×00;
DDRB=0×08;

// Port C initialization
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=Out Func6=Out Func7=Out
// State0=T State1=T State2=T State3=T State4=T State5=0 State6=0 State7=0
PORTC=0×00;
DDRC=0xE0;

// Port D initialization
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=Out Func6=In Func7=In
// State0=T State1=T State2=T State3=T State4=T State5=0 State6=T State7=T
PORTD=0×00;
DDRD=0×20;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Phase correct PWM top=FFh
// OC0 output: Non-Inverted PWM
TCCR0=0×60;
TCNT0=0×00;
OCR0=0×00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 2000.000 kHz
// Mode: Ph. correct PWM top=00FFh
// OC1A output: Non-Inv.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0×81;
TCCR1B=0×02;
TCNT1H=0×00;
TCNT1L=0×00;
OCR1AH=0×00;
OCR1AL=0×00;
OCR1BH=0×00;
OCR1BL=0×00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0×00;
TCCR2=0×00;
TCNT2=0×00;
OCR2=0×00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
GICR|=0×00;
MCUCR=0×00;
MCUCSR=0×00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0×00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
// Analog Comparator Output: Off
ACSR=0×80;
SFIOR=0×00;

while (1)
{
//clockwise speed control
for(CW_var= 1; CW_var < 255; CW_var++)
{
PORTC.7 = 1;
OCR1A = CW_var;
delay_ms(500);
}
OCR1A = 0;
delay_ms(10000);
};
}

Posted in Aplikasi AVR | Leave a comment

Send SMS with Wavecom Modem via Microcontroller

Microcontroller is used here as a regulator of when and where thenumber of SMS to be sent and the content of SMS which will be sent.We need a GPRS modem M1206B (Q2403A) for connecting to the GSM network.


Microcontroller sends commands to the M1206B AT-Command viaRS232 serial communication. Then the GSM modem will send datain accordance with the AT-Command who receives.

What is AT-Command?
AT-Command is an instruction which is recognized by the GSMmodem to be willing to perform its functions. GSM modems can be either HP or M1206B as above.

GSM Modem Baudrate Setting:
The first thing which is done so that the microcontroller cancommunicate with a GPRS modem which equate Baudrate.M1206B default baudrate = 115200 bps. We use it to change thedefault Windows Hyperterminal.
Go to the Start -> AllProgram -> Accessories-> Communications->Hyper terminal.
in boxdialog Connect to select the COM port that you connect to theM1206B (if I pake COM1). Then in boxdialog COM Properties, change the Bits per second to 115200 and change the Flow Control to None

 

/*****************************************************
Chip type : ATmega8535
Program type : Application
Clock frequency : 4.000000 MHz
*****************************************************/
#include <mega8535.h>
// Standard Input/Output functions
#include <stdio.h>
#include <delay.h>
#define sensor0 PINA.0
#define sensor1 PINA.1
void main(void)
{
PORTA=0xff;
DDRA=0×00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0×00;
UCSRB=0×18;
UCSRC=0×86;
UBRRH=0×00;
UBRRL=0×19;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0×80;
SFIOR=0×00;
printf(“AT+CMGS=”);
putchar(‘”‘);
printf(“03160542812″); //—>This mobile phone which is intended
putchar(‘”‘);
putchar(‘,’);
putchar(13);
putchar(10);
printf(“test Send SMS OK”);
putchar(26);
delay_ms(500);
while (1)
{
// Place your code here
if(sensor0==0)
{
printf(“AT+CMGS=”);
putchar(‘”‘);
printf(“03160542812″); //—>This mobile phone which is intended
putchar(‘”‘);
putchar(‘,’);
putchar(13);
putchar(10);
printf(“sensor0 AKTIF”);
putchar(26);
}
if(sensor1==0)
{
printf(“AT+CMGS=”);
putchar(‘”‘);
printf(“03160542812″); //—>This mobile phone which is intended
putchar(‘”‘);
putchar(‘,’);
putchar(13);
putchar(10);
printf(“sensor1 AKTIF”);
putchar(26);
}
delay_ms(500);
};
}

 

Posted in Aplikasi AVR | Leave a comment

Send SMS with Wavecom Modem via Hyperterminal

Now its time completely automated and practical, nothing to liveSMS, including SMS application with a microcontroller, which is very useful as a monitoring, warning, control, or other remoteapplications. For example, remote control door; warning if there is a fire or gas leak at home, monitoring temperature, humidity, gassensors or other sensors, or for automatic telephone answering machine, and so on.

Modem that I use for the application of SMS or Short Message Service is Wavecom Fastrack GSM modem that looks like this:

The surplus of GSM modem is, if you want to submit the data no longer form a complex PDU, simply by using common serial data transmission on the microcontroller, further described below.
Before the trial in mikrokonrtroler, we first test the modem inHyperterminal with settings as follows:

115 200 baud rate is the default setting for serial communicationWavecom Fastrack GSM modem is.
Then type AT modem to check the response, if the settings are correct then the modem will respond OK. Then type ATE1, whichserves to make the data that we echo the same type in the modem,the alias is sent back so that it can appear in Hyperterminal, the modem will respond OK.
 
To eliminate echo type ATE0, then if it is then typed other commands in Hyperterminal, then the writing will not be visible, which will be visible only return the response from the modem. While connected to the microcontroller, the echo must be deactivated.
If you want to send SMS type AT + CMGS = <nomor tujuan>, after enter the content type that SMS would be sent, and to send CTRL + Ztype, a result like this:

 

Posted in Aplikasi AVR | Leave a comment

11. Serial Communication RS232

USART

INTRODUCTION:

    In lamest terms the USART (Universal Synchronous and Asynchronous serial Receiver and Transmitter) is a 0-5V version of the RS232 serial protocol.  A lot of devices communicate over this protocol and several devices exist to boost the USART to RS232 levels so that you could talk to serial devices.  RS232 was the protocol of choice before USB and many industrial and aviation hardware still contain RS232 ports.
THEORY OF OPERATION:
    The USART outputs serial data over the TX (transmit) pin and listens for data on the RX (Receive)  pin using TTL voltage levels (0-5V).  The protocol is fairly simple, if the settings on both devices match they should be able to talk to each other.
    The AVR datasheet gives a good writeup of the protocol so I’m not going to go into any detail about it.  I however say that the most common setting is:
Baud rate: 9600
Data bits: 8
Parity: None
Stop Bit: 1
Flow Control: None
    The difference between the registers on the ATmega8 and the ATmega168/328 is mostly a “0″ at the end of most register names.  I’m guessing that this is preparations for AVRs with multiple USART ports.  In this section I am going to change up for format a bit and show both registers under the same table.
 7 bit 6 bit  5 bit  4 bit  3 bit  2 bit  1 bit  0 bit  AVR Type
UCSRA RXC TXC UDRE FE DOR PE U2X MPCM ATmega8
UCSR0A RXCn TXCn UDREn FEn DORn PEn U2Xn MPCMn ATmega168/328

USART Control and Status Register A

 7 bit 6 bit  5 bit  4 bit  3 bit  2 bit  1 bit  0 bit  AVR Type
UCSRB RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8 ATmega8
UCSR0B RXCIE0 TXCIE0 UDRIE0 RXEN0 TXEN0 UCSZ02 RXB80 TXB80 ATmega168/328

USART Control and Status Register B

 7 bit 6 bit  5 bit  4 bit  3 bit  2 bit  1 bit  0 bit  AVR Type
UCSRC URSEL* UMSEL UPM1 UPM0 USBS UCSZ1 UCSZ0 UCPOL ATmega8
UCSR0C UMSELn1 UMSELn0 UPMn1 UPMn0 USBSn UCSZn1 UCSZn0 UCPOLn ATmega168/328

USART Control and Status Register C (* ATmega8 only)



 UMSEL  MODE 
0  Asynchronous Operation
1  Synchronous Operation
UMSEL Bit Settings (ATmega8 ONLY)


 UMSELn  UMSELn0  MODE
0 0  Asynchronous Operation
0 1  Synchronous Operation
1 0  (Reserved)
1 1  Master SPI Mode
UMSEL Bits Settings (ATmega168/328 Only)


 UPM1 (UPMn1)  UPM0 (UPM00) 

 PARITY MODE

0 0  Disabled
0 1  Reserved
1 0  Enabled, Even Parity
1 1  Enabled, Odd Parity
UPM (UPMn) Bit Settings


 USBS (USBS0)  Stop Bit(s)
0  1 bit
1  2 bit
USBS (USBS0) Bit Settings


 UCSZ2 (UCSZ02)  UCSZ1 (UCSZ01)  UCSZ0 (UCSZ00)  CHAR SIZE
0 0 0  5-bit
0 0 1  6-bit
0 1 0  7-bit
0 1 1  8-bit
1 0 0  Reserved
1 0 1  Reserved
1 1 0  Reserved
1 1 1  9-bit
UCSZ (UCSZn) Bit Settings


 UCPOL (UCPOLn)  TX Data Change  RX Data Sampled
0  Rising XCK Edge  Falling XCK Edge
1  Falling XCK Edge  Rising XCK Edge

UCPOL (UCPOL0) Bit Settings



 NAME  FUNCTION  SIZE  AVR TYPE
 UDR  Transmit / Receive Buffer  8 bit  ATmega8
 UDRn  Transmit / Receive Buffer  8 bit  ATmega328
 UBRRH  Baud Rate Register upper 4 bits  8 bit (3:0 used)  ATmega8
 UBRRnH  Baud Rate Register upper 4 bits  8 bit (3:0 used)  ATmega328
 UBRRL  Baud Rate Register Lower 8 bits  8 bit  ATmega8
 UBRRRnL  Baud Rate Register Lower 8 bits  8 bit  ATmega328

Other Registers


    So we have 3 Control and Status Registers (A, B & C).  Register A mainly contains status data.  Register B contains all interrupt settings and some none optional settings.  Register C contains all the configuration settings.
Transmitting Speed:
    For whatever reason the USART circuitry within the AVR really likes clock frequencies divisible by 1.8432MHz.  If your clock source is not divisible by 1.8432 you will have a percentage of error which will result in unstable communications.  The datasheet provides a large table called ”Examples of Baud Rate Setting” under the “USART” section which shows you the Error rate for all baud rates at given System Clock Settings, As long as the value is less then 0.5% your communication should be solid.  The chart also shows you the value that you want to write into the UBRR register.
    Most programmers choose to use a a formula in code to figure this out, this way they don’t have to reference the datasheet every time they need to change the speed.  The formula for this is:
ubrr_value = (Clock_Speed[a.k.a. FOSC] / 16 / Baud_Rate) – 1
    Just remember this value can be up to 12 bits in size, and has to be written across 2 different 8 bit registers, so make sure you make ubrr_value an integer.  Then we need to split the register the value into the upper and lowest 8 bits into the UBRRL(UBRRnL) register and the highest 4 bits into the UBRRH (UBRRnH) register.
Special Note for ATmega8:

    The C register (UCRSC) on the ATmega8 contains URSEL (USART Select) bit.  In order to write data to the UBRRH register the URSEL bit needs to be LOW(0), and in order to write to write to register C (UCRSC) you need to set this bit (URSEL) to HIGH(1).  This does not exist on the ATmega168/328.
Frame Formats:
    Now that we know how fast to send the data, we have to figure out how to format our data so that the other device could understand the data that we are sending it (and so that we could understand the data that is being sent to us).  All of our configuration options are located in Register C (UCRSC or UCSRnC).
    The first thing we want to do is setup the character size which is controlled by the UCSZ1(UCSZn1) bit and UCSZ0(UCSZn0) bit which are located in Register C, and the oddball UCSZ2(UCSZn2) in register B (which is used for 9 bit data, this rare).
    The next thing we want to setup is the parity which is set by the UPM1 (UPMn1) bit and the UPM0 (UPMn0) bit, the most common parity setting is NONE so we can simply leave the bits LOW (0).
    Lastly the stop bit which is controlled by the USBS(USBS0) bit, again, 2 bit stop is rarely used so we usually leave this bit LOW(0) for 1bit.
Enabling Communication:
    Now this is something really cool, you can enable the transmission and receive functionality independently.  Both the RXEN(RXENn) bit (Receive Enable) and the TXEN(TXENn) bit (Transmitter Enable) are both located in the B Register (UCSRB or UCSRnB)
Transmit and Receive Buffer:
   This is something that blows my mind, the Transmit and Receive share the same register the UDR(UDRn).  Whats even more wild is that the Receive buffer can store 2 characters.  While this sound a bit crazy it does work.  We have to keep 2 things in mind in order to get flawless operation.  The buffer has to be clear in order to transmit.  So all we really have to do is check to see if the buffer is clear UDR(UDRn) and if it is we can transmit.  If it is not clear, reading the data will will cause it clear (much like reading the ADC clears it).
Status and Fault Bits:
    Now that we have our communication up and running we need to be able to monitor its status and any faults.  This is done by Register A (UCSRA or UCSRnA)
    The RXC(RXCn) bit (Receive Complete) will be set to HIGH(1) when data has been received and is available in the UDR (UDR0) buffer.
    The TXC(TXCn) bit (Transmission Complete) is set to HIGH(1) when a transmission is completed and there is no other data to send.
    The UDRE(UDREn) is set to HIGH(1) when the buffer (UDR/UDRn) is empty, and therefore ready to be written.
    The FE(FE0) bit (Frame Error) is set HIGH(1) if the next bit in the receive buffer has a Frame Error.  If the recieve buffer is read the FE(FE0) bit is cleared.
    The DOR(DORn) bit (Data OverRun) is set HIGH(1) if the receive buffer is full (2 characters).  The bit is cleared LOW(0) if the UDR(UDRn) is read.
    The UPE(UPEn) bit (Parity Error) is set HIGH(1) if the next character in the receive buffer has a parity error.  The bit is cleared LOW(0) if the UDR(UDRn) is read.
Interrupts:Finally, the interrupts.  There are 3 of them.  Receive Complete, Data Register Empty and, Transmit Complete.  All 3 of the bits required to set these interrupts are located in the B register (UCSRB or UCSRnB).  Setting the RXCIE(RXCIEn) bit (Receive Complete Interrupt Enable) to HIGH(1) enables the Receive interrupt.  Setting the TXCIE(TXCIEn) bit (Transmit Complete Interrupt Enable)  to HIGH(1) enables the Transmit interrupt.  Setting the UDRIE(UDRIEn) bit (USART Data Register Empty Interrupt Enable) enables the USART Data Register Empty Interrupt.

    IMPORTANT:  If you enable the receiver interrupt you MUST READ the UDR register in order to clear the interrupt flag.  If you fail to read the UDR register in the interrupt routine the interrupt flag will not be cleared.  This will cause the program to will jump back to the main routine, execute 1 line of code (Remember this back from Interrupt Tutorial) then jump back into the interrupt routine.

1. Experiment send singgle character ‘A’

#include <mega8535.h>
#include <stdio.h>

void main(void)
{

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0×00;
UCSRB=0×18;
UCSRC=0×86;
UBRRH=0×00;
UBRRL=0×47;

while (1)
{
putchar(‘a’);
}
}

2. Experiment send char ‘a’ and Enter character

#include <mega16.h>
#include <stdio.h>

void main(void)
{

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0×00;
UCSRB=0×18;
UCSRC=0×86;
UBRRH=0×00;
UBRRL=0×47;

while (1)
{
putchar(‘a’);
putchar(0x0d);
putchar(0x0a);
}
}

3. Experiment send string

#include <mega16.h>
#include <stdio.h>

void main(void)
{
flash char *pesan,*pesan1; // jangan lupa di beri asterix utk tipe data string
char i;

PORTA=0xff;
DDRA=0xff;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0×00;
UCSRB=0×18;
UCSRC=0×86;
UBRRH=0×00;
UBRRL=0×47;

pesan=”Ini pesan teks”;
pesan1=”Ini pesan 1″;

while (1)
{
putsf(pesan);
putsf(“Ini pesan kedua”);

i=0;
while(pesan1[i]!=0)
{
putchar(pesan1[i]);
i++;
}
}

}

 

 

Posted in Tutorial AVR Codevisioin | Leave a comment