1. Trang chủ
  2. » Giáo án - Bài giảng

Project mẫu AVR 2

56 93 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 56
Dung lượng 3,23 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

// Noise Canceler: Off // Input Capture on Rising Edge // Timer 1 Overflow Interrupt: On // Input Capture Interrupt: On // Compare A Match Interrupt: Off // Compare B Match Interrupt: Of

Trang 1

Project mẫu AVR (phần 2)

Viết bởi: le_hieu_29 - 19/03/2012

13.Pulse period by timer1

Sơ đồ mạch điện:

Sơ đồ mạch điện.

Chương trình mẫu:

Chip type : ATmega32

Program type : Application

Clock frequency : 6.000000 MHz

Memory model : Small

External SRAM size : 0

Data Stack size : 512

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

#include "mega32.h"

// Standard Input/Output functions

#include "stdio.h"

Trang 2

unsigned char ov_counter;

unsigned int starting_edge, ending_edge;

unsigned long clocks;

unsigned int period_out;

bit done = 0;

// Timer 1 overflow interrupt service routine

interrupt [TIM1_OVF] void timer1_ovf_isr(void)

{

// Place your code here

++ov_counter;

}

// Timer 1 input capture interrupt service routine

interrupt [TIM1_CAPT] void timer1_capt_isr(void)

{

// Place your code here

ending_edge = (unsigned int)ICR1H*256 + (unsigned int)ICR1L;

clocks = (unsigned long)ending_edge \

// Declare your local variables here

// Input/Output Ports initialization

// Port A initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTA=0x00;

DDRA=0x00;

// Port B initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTB=0x00;

DDRB=0x00;

// Port C initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC=0x00;

DDRC=0x00;

Trang 3

// Port D initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTD=0x00;

DDRD=0x00;

// Timer/Counter 0 initialization

// Clock source: System Clock

// Clock value: Timer 0 Stopped

// Mode: Normal top=FFh

// OC0 output: Disconnected

// Mode: Normal top=FFFFh

// OC1A output: Discon

// OC1B output: Discon

// Noise Canceler: Off

// Input Capture on Rising Edge

// Timer 1 Overflow Interrupt: On

// Input Capture Interrupt: On

// Compare A Match Interrupt: Off

// Compare B Match Interrupt: Off

// Clock source: System Clock

// Clock value: Timer 2 Stopped

// Mode: Normal top=FFh

// OC2 output: Disconnected

Trang 4

// USART Mode: Asynchronous

// USART Baud rate: 9600

// Analog Comparator initialization

// Analog Comparator: Off

// Analog Comparator Input Capture by Timer/Counter 1: OffACSR=0x80;

Trang 5

Sơ đồ mạch điện.

Chương trình mẫu:

Chip type : ATmega32

Program type : Application

Clock frequency : 7.372800 MHz

Memory model : Small

External SRAM size : 0

Data Stack size : 512

// Declare your global variables here

bit adc_done = 0; // Global ADC done flag bit

// ADC interrupt service routine

interrupt [ADC_INT] void adc_isr(void)

{

unsigned int adc_result;

// Read the AD conversion result

adc_result = ADCW;

// Always clear ADC IF bit

Trang 6

ADCSRA |= 0x10;

// print this value

printf("Done interrupt! ADC value = %d\n\r",adc_result);

// Set done bit

adc_done = 1;

}

// Trigger ADC conversion

void adc_trigger_conv(unsigned char adc_input)

// Declare your local variables here

// Input/Output Ports initialization

// Port A initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTA=0x00;

DDRA=0x00;

// Port B initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTB=0x00;

DDRB=0x00;

// Port C initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC=0x00;

DDRC=0x00;

// Port D initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTD=0x00;

DDRD=0x00;

// Timer/Counter 0 initialization

// Clock source: System Clock

// Clock value: Timer 0 Stopped

// Mode: Normal top=FFh

Trang 7

// OC0 output: Disconnected

TCCR0=0x00;

TCNT0=0x00;

OCR0=0x00;

// Timer/Counter 1 initialization

// Clock source: System Clock

// Clock value: Timer 1 Stopped

// Mode: Normal top=FFFFh

// OC1A output: Discon

// OC1B output: Discon

// Noise Canceler: Off

// Input Capture on Falling Edge

// Timer 1 Overflow Interrupt: Off

// Input Capture Interrupt: Off

// Compare A Match Interrupt: Off

// Compare B Match Interrupt: Off

// Clock source: System Clock

// Clock value: Timer 2 Stopped

// Mode: Normal top=FFh

// OC2 output: Disconnected

Trang 8

// USART Baud rate: 9600

// Analog Comparator initialization

// Analog Comparator: Off

// Analog Comparator Input Capture by Timer/Counter 1: OffACSR=0x80;

SFIOR=0x00;

// ADC initialization

// ADC Clock frequency: 115.200 kHz

// ADC Voltage Reference: AREF pin

Trang 9

Sơ đồ mạch điện.

Chương trình mẫu:

Chip type : ATmega32

Program type : Application

Clock frequency : 7.372800 MHz

Memory model : Small

External SRAM size : 0

Data Stack size : 512

// Here, we choose ADC0 as Neg Input

// ADC1 as Pos Input

// Gain set to 10x

// Look up on table in datasheet > MUX4:0 = 0b01001

#define ADC_DIFF_TYPE (0b01001)

// Convert value from ADC data register with its sign (+/-)

int convert_adc_value(unsigned int adc_10bit_input_value)

Trang 10

int result;

result = (int)(adc_10bit_input_value & 0x1FF);

result = (adc_10bit_input_value & (1 << 9)) ? (-(512 - result)) : result;

ADMUX = ADC_VREF_TYPE | ADC_DIFF_TYPE;

// Start the AD conversion

ADCSRA|=0x40;

// Wait for the AD conversion to complete

while ((ADCSRA & 0x10)==0);

// Declare your local variables here

unsigned int u16_adc;

DDRA=0x00;

// Port B initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTB=0x00;

DDRB=0x00;

// Port C initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC=0x00;

DDRC=0x00;

// Port D initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTD=0x00;

DDRD=0x00;

Trang 11

// Timer/Counter 0 initialization

// Clock source: System Clock

// Clock value: Timer 0 Stopped

// Mode: Normal top=FFh

// OC0 output: Disconnected

TCCR0=0x00;

TCNT0=0x00;

OCR0=0x00;

// Timer/Counter 1 initialization

// Clock source: System Clock

// Clock value: Timer 1 Stopped

// Mode: Normal top=FFFFh

// OC1A output: Discon

// OC1B output: Discon

// Noise Canceler: Off

// Input Capture on Falling Edge

// Timer 1 Overflow Interrupt: Off

// Input Capture Interrupt: Off

// Compare A Match Interrupt: Off

// Compare B Match Interrupt: Off

// Clock source: System Clock

// Clock value: Timer 2 Stopped

// Mode: Normal top=FFh

// OC2 output: Disconnected

Trang 12

// USART initialization

// Communication Parameters: 8 Data, 1 Stop, No Parity// USART Receiver: On

// USART Transmitter: On

// USART Mode: Asynchronous

// USART Baud rate: 9600

// Analog Comparator initialization

// Analog Comparator: Off

// Analog Comparator Input Capture by Timer/Counter 1: OffACSR=0x80;

SFIOR=0x00;

// ADC initialization

// ADC Clock frequency: 115.200 kHz

// ADC Voltage Reference: AREF pin

Trang 13

Sơ đồ mạch điện.

Chương trình mẫu:

Chip type : ATmega32

Program type : Application

Clock frequency : 7.372800 MHz

Memory model : Small

External SRAM size : 0

Data Stack size : 512

-// ADSRA register definitions

#define ADEN (1 << 7) // ADC Enable

#define ADSC (1 << 6) // ADC Start Conversion

#define ADATE (1 << 5) // ADC Auto Trigger Enable

#define ADIF (1 << 4) // ADC Interrupt Flag

#define ADIE (1 << 3) // ADC Interrupt Enable

#define ADPS2 (1 << 2) // ADC Prescale bit 2

#define ADPS1 (1 << 1) // ADC Prescale bit 1

#define ADPS0 (1) // ADC Prescale bit 0

// SFIOR register definitions

#define ADTS2 (1 << 7) // ADC Trigger Source bit 2

Trang 14

#define ADTS1 (1 << 6) // ADC Trigger Source bit 1

#define ADTS0 (1 << 5) // ADC Trigger Source bit 0

#define ADTS_MASK (ADTS2 | ADTS1 | ADTS0) // ADC Trigger Source Bits Mask// GIFR register definitions

#define INT0 (1 << 6) // INT0

#define ADC_VREF_TYPE 0x00

// Global varables

-// 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

printf("Conversion done! Value = %d\n\r",adc_data);

// Clear INT0 flag for next auto trigger by INT0

GIFR = INT0;

}

// Configure ADC channel that conversion will be trigger to

// convert ADC for

void config_adc_channel(unsigned char adc_input)

// Declare your local variables here

unsigned char temp;

// Input/Output Ports initialization

// Port A initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTA=0x00;

DDRA=0x00;

// Port B initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTB=0x00;

DDRB=0x00;

// Port C initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T

Trang 15

// Timer/Counter 0 initialization

// Clock source: System Clock

// Clock value: Timer 0 Stopped

// Mode: Normal top=FFh

// OC0 output: Disconnected

TCCR0=0x00;

TCNT0=0x00;

OCR0=0x00;

// Timer/Counter 1 initialization

// Clock source: System Clock

// Clock value: Timer 1 Stopped

// Mode: Normal top=FFFFh

// OC1A output: Discon

// OC1B output: Discon

// Noise Canceler: Off

// Input Capture on Falling Edge

// Timer 1 Overflow Interrupt: Off

// Input Capture Interrupt: Off

// Compare A Match Interrupt: Off

// Compare B Match Interrupt: Off

// Clock source: System Clock

// Clock value: Timer 2 Stopped

// Mode: Normal top=FFh

// OC2 output: Disconnected

Trang 16

// INT0 Mode: Falling Edge

// INT1: Off

// INT2: Off

GICR=0x00; // Disable INT0 Request to Global Interrupt

MCUCR=0x02; // Falling Edge on INT0

MCUCSR=0x00;

GIFR=0x40; // Clear INT0 Interrupt Flag

// Timer(s)/Counter(s) Interrupt(s) initialization

// USART Mode: Asynchronous

// USART Baud rate: 9600

// Analog Comparator initialization

// Analog Comparator: Off

// Analog Comparator Input Capture by Timer/Counter 1: OffACSR=0x80;

SFIOR=0x00;

// ADC initialization

// ADC Clock frequency: 115.200 kHz

// ADC Voltage Reference: AREF pin

ADMUX=ADC_VREF_TYPE;

// Enable Auto trigger conversion ADCSRA = 0x8E | ADATE;

-// Configure trigger source

-// Read out and mask ADTS[2 0] bit in the current status of SFIORtemp = SFIOR & (~(ADTS_MASK));

// Choose External Interrupt 0 (see datasheet)

Trang 17

// Place your code here

Chip type : ATmega32

Program type : Application

Clock frequency : 7.372800 MHz

Memory model : Small

External SRAM size : 0

Data Stack size : 512

Trang 18

#define ADC_VREF_TYPE 0x00

// Read the AD conversion result

unsigned int read_adc(unsigned char adc_input)

{

ADMUX=adc_input|ADC_VREF_TYPE;

// Start the AD conversion

ADCSRA|=0x40;

// Wait for the AD conversion to complete

while ((ADCSRA & 0x10)==0);

// Declare your local variables here

unsigned int adc_value; // ADC result

float lm35_voltage; // Voltage on Output of LM35

unsigned int lm35_temp; // Current temperature

char buff[10]; // temp buff hold conversion from float number to string// Input/Output Ports initialization

// Port A initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTA=0x00;

DDRA=0x00;

// Port B initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTB=0x00;

DDRB=0x00;

// Port C initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC=0x00;

DDRC=0x00;

// Port D initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTD=0x00;

DDRD=0x00;

// Timer/Counter 0 initialization

// Clock source: System Clock

// Clock value: Timer 0 Stopped

// Mode: Normal top=FFh

Trang 19

// OC0 output: Disconnected

TCCR0=0x00;

TCNT0=0x00;

OCR0=0x00;

// Timer/Counter 1 initialization

// Clock source: System Clock

// Clock value: Timer 1 Stopped

// Mode: Normal top=FFFFh

// OC1A output: Discon

// OC1B output: Discon

// Noise Canceler: Off

// Input Capture on Falling Edge

// Timer 1 Overflow Interrupt: Off

// Input Capture Interrupt: Off

// Compare A Match Interrupt: Off

// Compare B Match Interrupt: Off

// Clock source: System Clock

// Clock value: Timer 2 Stopped

// Mode: Normal top=FFh

// OC2 output: Disconnected

Trang 20

// USART Baud rate: 9600

// Analog Comparator initialization

// Analog Comparator: Off

// Analog Comparator Input Capture by Timer/Counter 1: Off

ACSR=0x80;

SFIOR=0x00;

// ADC initialization

// ADC Clock frequency: 115.200 kHz

// ADC Voltage Reference: AREF pin

printf("Voltage on output of LM35: %sV\n\r",buff);

// Calc to temperature - note that LM35 has Linear +10.0 mV/C scale factor lm35_temp = (unsigned int)((float)adc_value*500/1023 + 0.5);

printf("Current Temperature: %d.C\n\r",lm35_temp);

Trang 21

Sơ đồ mạch điện.

Chương trình mẫu:

Chip type : ATmega32

Program type : Application

Clock frequency : 7.372800 MHz

Memory model : Small

External SRAM size : 0

Data Stack size : 512

// Read the AD conversion result

unsigned int read_adc(unsigned char adc_input)

Trang 22

while ((ADCSRA & 0x10)==0);

ADCSRA|=0x10;

return ADCW;

}

// Declare your global variables here

unsigned int adc_value;

DDRA=0x00;

// Port B initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTB=0x00;

DDRB=0x00;

// Port C initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC=0x00;

DDRC=0x00;

// Port D initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTD=0x00;

DDRD=0x00;

// Timer/Counter 0 initialization

// Clock source: System Clock

// Clock value: Timer 0 Stopped

// Mode: Normal top=FFh

// OC0 output: Disconnected

TCCR0=0x00;

TCNT0=0x00;

OCR0=0x00;

// Timer/Counter 1 initialization

// Clock source: System Clock

// Clock value: Timer 1 Stopped

// Mode: Normal top=FFFFh

// OC1A output: Discon

Trang 23

// OC1B output: Discon.

// Noise Canceler: Off

// Input Capture on Falling Edge

// Timer 1 Overflow Interrupt: Off

// Input Capture Interrupt: Off

// Compare A Match Interrupt: Off

// Compare B Match Interrupt: Off

// Clock source: System Clock

// Clock value: Timer 2 Stopped

// Mode: Normal top=FFh

// OC2 output: Disconnected

// USART Mode: Asynchronous

// USART Baud rate: 9600

// Analog Comparator initialization

// Analog Comparator: Off

// Analog Comparator Input Capture by Timer/Counter 1: Off

Trang 24

SFIOR=0x00;

// ADC initialization

// ADC Clock frequency: 115.200 kHz

// ADC Voltage Reference: AREF pin

Trang 25

Chương trình mẫu:

Chip type : ATmega32

Program type : Application

Clock frequency : 7.372800 MHz

Memory model : Small

External SRAM size : 0

Data Stack size : 512

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

#include "mega32.h"

#include "delay.h"

#define ADC_VREF_TYPE 0x00

// Read the AD conversion result

unsigned int read_adc(unsigned char adc_input)

{

ADMUX=adc_input|ADC_VREF_TYPE;

// Start the AD conversion

ADCSRA|=0x40;

// Wait for the AD conversion to complete

while ((ADCSRA & 0x10)==0);

// Declare your local variables here

unsigned int vr_value; // ADC result of voltage getting from VR

unsigned long int led_bar_value; // Temp led bar value after calculating

// Input/Output Ports initialization

// Port A initialization

// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In

// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T

Trang 26

// Clock source: System Clock

// Clock value: Timer 0 Stopped

// Mode: Normal top=FFh

// OC0 output: Disconnected

TCCR0=0x00;

TCNT0=0x00;

OCR0=0x00;

// Timer/Counter 1 initialization

// Clock source: System Clock

// Clock value: Timer 1 Stopped

// Mode: Normal top=FFFFh

// OC1A output: Discon

// OC1B output: Discon

// Noise Canceler: Off

// Input Capture on Falling Edge

// Timer 1 Overflow Interrupt: Off

// Input Capture Interrupt: Off

// Compare A Match Interrupt: Off

// Compare B Match Interrupt: Off

// Clock source: System Clock

// Clock value: Timer 2 Stopped

// Mode: Normal top=FFh

// OC2 output: Disconnected

ASSR=0x00;

TCCR2=0x00;

TCNT2=0x00;

OCR2=0x00;

Trang 27

// External Interrupt(s) initialization

// Analog Comparator initialization

// Analog Comparator: Off

// Analog Comparator Input Capture by Timer/Counter 1: Off

ACSR=0x80;

SFIOR=0x00;

// ADC initialization

// ADC Clock frequency: 115.200 kHz

// ADC Voltage Reference: AREF pin

// Here, we have 24 leds, so we assume 24 steps or 24 level

// Note that, the result is always be rounded DOWN, so 0.5 is added to this result vr_value = (unsigned int)(((float)vr_value * 24) / 1023 + 0.5);

// we must fill '1' value from 0 to current step

led_bar_value = (1 << ((unsigned long int)vr_value)) - 1;

// Send each byte of this value to each port

PORTB = led_bar_value & 0xFF;

PORTC = (led_bar_value >> 8) & 0xFF;

PORTD = (led_bar_value >> 16) & 0xFF;

// Delay for a while

Trang 28

Sơ đồ mạch điện.

Chương trình mẫu:

Chip type : ATmega32

Program type : Application

Clock frequency : 7.372800 MHz

Memory model : Small

External SRAM size : 0

Data Stack size : 512

Ngày đăng: 16/12/2019, 17:09

TỪ KHÓA LIÊN QUAN

w