Code nạp cho vi xử lý

Một phần của tài liệu Luận văn thạc sĩ Vật lý kỹ thuật: Thiết kế và chế tạo máy đo độ ồn dựa trên cảm biến âm microphone electret (Design and make a sound level meter by using a transducer electret microphone) (Trang 79 - 94)

CHƯƠNG TRÌNH CHÍNH:

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

This program was produced by the CodeWizardAVR V2.05.6

Automatic Program Generator

© Copyright 1998-2012 Pavel Haiduc, HP InfoTech s.r.l.

http://www.hpinfotech.com Project : SOUND LEVEL METTER Version : 3

Date : 3/2/2014 Author : Cracked By PerTic@n (Evaluation) V1.0 - SonSivRi.to

Company : SPKT Comments:

Chip type : ATmega8 Program type : Application AVR Core Clock frequency: 8.000000 MHz Memory model : Small

External RAM size : 0 Data Stack size : 256

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

/*Khai bao thu vien su dung*/

#include <mega8.h>

#include <delay.h>

// Alphanumeric LCD functions

#include <alcd.h>

#include <stdio.h>

#ifndef RXB8

#define RXB8 1

#endif

#ifndef TXB8

#define TXB8 0

#endif

#ifndef UPE

#define UPE 2

#endif

#ifndef DOR

#define DOR 3

#endif

#ifndef FE

#define FE 4

#endif

#ifndef UDRE

#define UDRE 5

#endif

#ifndef RXC

#define RXC 7

#endif

#define FRAMING_ERROR (1<<FE)

#define PARITY_ERROR (1<<UPE)

#define DATA_OVERRUN (1<<DOR)

#define DATA_REGISTER_EMPTY (1<<UDRE)

#define RX_COMPLETE (1<<RXC)

// Standard Input/Output functions

#define ADC_VREF_TYPE 0x00

#include <KhaiBao.c>

//KHAI BAO CHUONG TRINH CON SU DUNG unsigned int read_adc(unsigned char adc_input);

#include <InterruptUSART.c>

#include <Function.c>

// Timer 0 overflow interrupt service routine interrupt [TIM0_OVF] void timer0_ovf_isr(void) //Ngat tran Timer 0 {

// Place your code here TCNT0=0x00;

if(data=='a') // Neu data=='a' thi ket noi may tinh {

connect=1;

} else if(data=='b') // Neu data=='b' thi ngat ket noi may tinh {

connect=0;

} else if(data=='1') // Neu data=='1' thi delay 0.5s {

bien_delay=1;

luu_delay = 250;

} else if(data=='2') // Neu data=='2' thi delay 1s {

bien_delay=2;

luu_delay = 620;

} else if(data=='3') // Neu data=='3' thi delay 2s {

bien_delay=3;

luu_delay = 1330;

} else if(data=='4') // Neu data=='4' thi delay 5s {

bien_delay=4;

luu_delay = 3500;

} else if(data=='5') // Neu data=='5' thi delay 10s {

bien_delay=5;

luu_delay = 7200;

} else if(data=='6') // Neu data=='6' thi delay 15s {

bien_delay=6;

luu_delay = 10700;

} else if(data=='7') // Neu data=='7' thi delay 20s {

bien_delay=7;

luu_delay = 14400;

} else if(data=='8') // Neu data=='8' thi delay 25s {

bien_delay=8;

luu_delay = 17900;

} else if(data=='9') // Neu data=='9' thi delay 30s {

bien_delay=9;

luu_delay = 21400;

}

if(!MODE && MODE!=mode_old) // Neu nha nut Mode thi tang MODE {

while (!MODE && MODE!=mode_old){};

bien_mode++;

if (bien_mode == 4) bien_mode =0;

} mode_old = MODE;

}

// Timer2 overflow interrupt service routine interrupt [TIM2_OVF] void timer2_ovf_isr(void) //Ngat tran Timer 2 {

// Place your code here TCNT2=0x00;

// Place your code here if(!LATCH && LATCH!=latch_old) //nut LATCH cung la nut DOWN

{ while (!LATCH && LATCH!=latch_old){};

tt=~tt;

} latch_old = LATCH;

if(!DOWN && DOWN!=down_old) //nut LATCH cung la nut DOWN {

while (!DOWN && DOWN!=down_old){};

if (bien_mode==1) //giam gia tri do lon nhat {

bien_edit_db_value -= 10;

if(bien_edit_db_value<20 ) bien_edit_db_value=140;

}

else if (bien_mode==3) //giam gia tri delay {

bien_delay --;

if(bien_delay<1 ) bien_delay=9;

truyen_delay = bien_delay + 64;

putchar(truyen_delay);

} } down_old = DOWN;

if(!UP && UP!=up_old) {

while (!UP && UP!=up_old){};

if (bien_mode==0) {

putchar(truyen_delay);

connect = ~ connect;

}

if (bien_mode==1) //tang gia tri do lon nhat {

bien_edit_db_value += 10;

if(bien_edit_db_value>140 ) bien_edit_db_value=20;

} else if (bien_mode==3) //tang gia tri delay {

bien_delay ++;

if(bien_delay>9 ) bien_delay=1;

truyen_delay = bien_delay + 64;

putchar(truyen_delay);

}

} up_old = UP;

} void main(void) {

#include <ConfigSet.c>

// Global enable interrupts #asm("sei")

// Characters/line: 16 lcd_init(16);

lcd_gotoxy(2,0);

lcd_puts("NOISE LEVEL");

lcd_gotoxy(5,1);

lcd_puts("METER");

delay_ms(700);

lcd_clear();

while (1) { // Place your code here delay=luu_delay;

thongbao(); //Thong bao ket noi voi may tinh adc_convert(0); //chuong trinh con chuyen doi ADC kenh 0, doc dB mode_setting(); //chuong trinh con cai dat che do

uart_db_tx(db_value); //chuong trinh con truyen du lieu len may tinh truyen_data=1;

if (bien_mode==0) delay_ms(delay); //delay thoi gian lay mau };

} ///////////////////////////////////////////////////////////////////////////////////////////////////////

CHƯƠNG TRÌNH CON:

Configset.c:

// 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=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 PORTB=0x06;

DDRB=0x01; // =0xFF la ngo ra

// Port C initialization // Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC=0xc0;

DDRC=0x30; // =0x00 la ngo vao // Port D initialization

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

PORTD=0x0c;

DDRD=0xf0;

// Timer/Counter 0 initialization // Clock source: System Clock // Clock value: 1000.000 kHz TCCR0=0x02;

TCNT0=0x00;

// Timer/Counter 1 initialization // Clock source: System Clock // Clock value: Timer1 Stopped // Mode: Normal top=0xFFFF // OC1A output: Discon.

// OC1B output: Discon.

// Noise Canceler: Off // Input Capture on Falling Edge // Timer1 Overflow Interrupt: Off // Input Capture Interrupt: Off // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off /*

- 3 bit TRONG THANH GHI TCCR1B: /CS12/CS11/CS10/=/1/0/1/

- DE BAT TIMER1: GAN TCCR1B=0x05.

- DE TAT TIMER1: GAN TCCR1B=0x00 - TIMER1 KHONG CO NGUON XUNG NHIP

Tan so T1 = FOSC(8Mh)/1024 = 7812.5 Hz = 7.813 KHz Thoi gian dem 1 don vi cua T1= 1/7.813 = 0.128 ms Delay 500ms thi T1 phai dem duoc 500/0.128 =3906 lan 5000/39063

VAY GIA TRI CAN DAT CHO VAO THANH GHI TCNT1 CUA TIMER 1 LA 65536- 39063=26500 = 0x9784

- BIT TOV1 TRONG THANH GHI TIFR SE DUOC BAT LEN KHI TIMER1 TRAN.

*/

TCCR1A=0x00;

TCCR1B=0x00;

TCNT1H=0x67;

TCNT1L=0x84;

ICR1H=0x00;

ICR1L=0x00;

OCR1AH=0x00;

OCR1AL=0x00;

OCR1BH=0x00;

OCR1BL=0x00;

// Timer/Counter 2 initialization // Clock source: System Clock

// Clock value: 1000.000 kHz // Mode: Normal top=0xFF // OC2 output: Disconnected ASSR=0x00;

TCCR2=0x02;

TCNT2=0x00;

OCR2=0x00;

// External Interrupt(s) initialization // INT0: Off

// INT1: Off MCUCR=0x00;

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

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

// USART Transmitter: On // USART Mode: Asynchronous // USART Baud Rate: 9600 UCSRA=0x00;

UCSRB=((1<<RXCIE)|(1<<RXEN)|(1<<TXEN)); //cho phep truyen/nhan UART UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); //quy dinh do dai du lieu = 9 bit

= 8 data + 1 stop UBRRH=0x00;

//

//UBRRL=6; // Toc do Boud = 76800 bps UBRRL=8; // Toc do Boud = 57600 bps //UBRRL=25; // Toc do Boud = 19200 bps //UBRRL=51; //Toc do Boud = 9600 bps

// Analog Comparator initialization // Analog Comparator: Off

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

SFIOR=0x00;

// ADC initialization // ADC Clock frequency: 500.000 kHz // ADC Voltage Reference: AREF pin ADMUX=ADC_VREF_TYPE & 0xff;

ADCSRA=0x84; //cho phep chuyen doi ADC

// SPI initialization // SPI disabled SPCR=0x00;

// TWI initialization

// TWI disabled TWCR=0x00;

////////////////////////////////////////////////////////////////////////////////////////

KhaiBao.c:

#define LATCH PINB.2 //nut nhan LATCH

#define UP PIND.2 //nut nhan UP

#define DOWN PIND.3

#define MODE PINB.1 //nut nhan cai dat che do bit latch_old, mode_old, up_old, down_old;

bit tt=0,connect=0; //bit trang thai cho LATCH unsigned long adc_value; //bien luu gia tri adc

unsigned long db_value; //bien luu gia tri da tinh toan ra dB //unsigned int db_value_ss;

unsigned char bien_edit_db_value=130;// bien_baud=2; // bien MaxValue vaf bieen Baud Rate

char data=' '; // bien data luu tam ky tu nhan duoc tu may tinh unsigned int edit_db_value1;

char bien_delay=1;

unsigned int delay,luu_delay=250;

unsigned char bien_mode=0;

unsigned int ng, tr, ch, dv; // Dung cho gia tri dB unsigned int Tram, Chuc, Donvi; // Dung cho Delay edit_db_value unsigned char truyen_data=0;

unsigned char truyen_delay=65;

////////////////////////////////////////////////////////////////////////////////////////

Function.c:

// Read the AD conversion result unsigned int read_adc(unsigned char adc_input) //Ham doc gia tri {

ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);

// Delay needed for the stabilization of the ADC input voltage delay_us(20);

// Start the AD conversion ADCSRA|=0x40;

// Wait for the AD conversion to complete while ((ADCSRA & 0x10)==0);

ADCSRA|=0x10;

return ADCW;

} // =================================

////================================

//thong bao khi giao dien ket noi voi phan cung void thongbao()

{ if (data=='b') {

lcd_gotoxy(0,0);

lcd_puts(" ");

lcd_gotoxy(0,1);

lcd_puts("PC DisConnecting");

delay_ms(500);

data=' ';

lcd_puts(" ");

} else if(data=='a') {

lcd_gotoxy(0,0);

lcd_puts(" ");

lcd_gotoxy(0,1);

lcd_puts(" PC Connecting ");

delay_ms(1000);

data=' ';

lcd_puts(" ");

} } // ==============================================================

//khoi block VU void lcd_block(unsigned char step,unsigned int adc_value) {

if (adc_value <1) { lcd_puts(" ");

} else if (adc_value >=1 && adc_value <step) { lcd_puts("| ");

} else if (adc_value >=step && adc_value <step*2){

lcd_puts("|| ");

} else if (adc_value >=step*2 && adc_value <step*3){

lcd_puts("||| ");

} else if (adc_value >=step*3 && adc_value <step*4){

lcd_puts("|||| ");

} else if (adc_value >=step*4 && adc_value <step*5){

lcd_puts("||||| ");

} else if (adc_value >=step*5 && adc_value <step*6){

lcd_puts("|||||| ");

} else if (adc_value >=step*6 && adc_value <step*7){

lcd_puts("||||||| ");

} else if (adc_value >=step*7 && adc_value <step*8){

lcd_puts("|||||||| ");

} else if (adc_value >=step*8 && adc_value <step*9){

lcd_puts("||||||||| ");

}

else if (adc_value >=step*9 && adc_value <step*10){

lcd_puts("|||||||||| ");

} else if (adc_value >=step*10 && adc_value <step*11){

lcd_puts("||||||||||| ");

} else if (adc_value >=step*11 && adc_value <step*12){

lcd_puts("|||||||||||| ");

} else if (adc_value >=step*12 && adc_value <step*13){

lcd_puts("||||||||||||| ");

} else if (adc_value >=step*13 && adc_value <step*14){

lcd_puts("|||||||||||||| ");

} else if (adc_value >=step*14 && adc_value <step*15){

lcd_puts("||||||||||||||| ");

} else if (adc_value >=step*15){

lcd_puts("||||||||||||||||");

};

} // ==============================================================

// Ham giai ma ASCII dB void lcd_putnum_db(unsigned int N) {

ng = N/1000;

tr = (N%1000)/100;

ch = (N%100)/10;

dv = N%10;

} // ==============================================================

// Ham giai ma ASCII void lcd_putnum(unsigned int N) {

Tram = N/100;

Chuc = (N%100)/10;

Donvi = N%10;

} // ==============================================================

//chuong tinh con hien thi gia tri am thanh len lcd void db_display (unsigned int nghin,unsigned int tram,unsigned int chuc,unsigned int donvi,)

{ if (nghin ==0){

if (tram ==0){

lcd_putchar(32);

lcd_putchar(32);

} else{

lcd_putchar(32);

lcd_putchar(tram + 48);

};

} else{

lcd_putchar(nghin + 48);

lcd_putchar(tram + 48);

};

lcd_putchar(chuc + 48);

lcd_putchar(46);

lcd_putchar(donvi + 48);

} // ==============================================================

//chuong trinh con hien thi delay + MaxValue dB void delay_display (unsigned int tram,unsigned int chuc,unsigned int donvi,) {

if (tram ==0){

if (chuc ==0){

lcd_putchar(32);

lcd_putchar(32);

lcd_putchar(32);

} else{

lcd_putchar(32);

lcd_putchar(32);

lcd_putchar(chuc + 48);

} } else{

lcd_putchar(32);

lcd_putchar(tram + 48);

lcd_putchar(chuc + 48);

} lcd_putchar(donvi + 48);

} // ==============================================================

//chuong trinh con cai dat che do void mode_setting ()

{ lcd_gotoxy(0,1);

switch (bien_mode) { case 0: // hien thi block lcd_block(45,adc_value1);

break;

case 1: //cai dat MaxValue lcd_puts(" Max Value:");

lcd_putnum(bien_edit_db_value);

delay_display(Tram,Chuc,Donvi);

lcd_puts(" ");

delay = 100;

break;

case 2: //cai dat Baud Rate lcd_puts(" Baud Rate 57600");

// lcd_puts(" Baud Rate 76800");

delay = 100;

break;

case 3:

{ //hien thi delay lcd_gotoxy(0,1);

switch (bien_delay){

case 1:

lcd_puts(" Delay: 0.5 s ");

luu_delay = 250;

break;

case 2:

lcd_puts(" Delay: 1 s ");

luu_delay = 620;

break;

case 3:

lcd_puts(" Delay: 2 s ");

luu_delay = 1330;

break;

case 4:

lcd_puts(" Delay: 5 s ");

luu_delay = 3500;

break;

case 5:

lcd_puts(" Delay: 10 s ");

luu_delay = 7200;

break;

case 6:

lcd_puts(" Delay: 15 s ");

luu_delay = 10700;

break;

case 7:

lcd_puts(" Delay: 20 s ");

luu_delay = 14400;

break;

case 8:

lcd_puts(" Delay: 25 s ");

luu_delay = 17900;

break;

case 9:

lcd_puts(" Delay: 30 s ");

luu_delay = 21400;

break;

} delay = 100;

break;

}

};

} // ==============================================================

void adc_convert(unsigned char channel) {

// Chon che do chot hoac khong chot gia tri lcd_gotoxy(1,0);

lcd_puts("Noise ");

lcd_gotoxy(13,0);

lcd_puts("dB");

lcd_gotoxy(0,0);

if (tt==0){

edit_db_value1 = bien_edit_db_value*10;

adc_value = read_adc(channel); // doc ADC kênh Channel, hi

if (adc_value >=293) {

adc_value1 =adc_value-293;

db_value = ((((adc_value-293)*edit_db_value1)/730)); // Tính toán giá giá tri chuyen doi

} else { adc_value1=0;

db_value =0;

} lcd_putchar(20); // xuat ky tu trang ra LCD }

else{

db_value = db_value;

lcd_putchar(0xff); // xuat ky tu boi den ra lcd };

// Hien thi gia tri len LCD lcd_gotoxy(7,0);

lcd_putnum_db(db_value); // giai ma db_display(ng,tr,ch,dv); //hien thi gia tri dB }

//===============================================

// truyen du lieu len may tinh void uart_db_tx(unsigned int db_value) {

if(connect==1) {

lcd_gotoxy(15,0);

lcd_putchar(0x7e);

if(truyen_data==1) {

if (db_value <=99) {

putchar('\n'); // phai gui cai nay truoc giao dien moi hien thi lau cho minh thay

putchar(ch +48);

putchar('.');

putchar(dv +48);

putchar('\t');

} else if ((db_value > 99) && (db_value <= 999)) { putchar('\n'); // phai gui cai nay truoc giao dien moi hien thi lau cho minh thay

putchar(tr +48);

putchar(ch +48);

putchar('.');

putchar(dv +48);

putchar('\t');

} else if ((db_value > 999)) { putchar('\n'); // phai gui cai nay truoc giao dien moi hien thi lau cho minh thay

putchar(ng +48); //gui ky tu nghin putchar(tr +48); //gui ky tu tram putchar(ch +48); //gui ky tu chuc putchar('.'); //gui dau cham putchar(dv +48); //gui ky tu don vi putchar('\t');

} } }

else { lcd_gotoxy(15,0);

lcd_putchar(20);

delay_ms(70);

} } //===================================

InterrupUSART.c:

//===================================

//NHAN DATA //=====================================

//USART Receiver buffer

#define RX_BUFFER_SIZE 8 char rx_buffer[RX_BUFFER_SIZE];

#if RX_BUFFER_SIZE <256 unsigned char rx_wr_index, rx_counter; //rx_rd_index,

#else unsigned int rx_wr_index,rx_counter; // rx_rd_index,

#endif bit rx_buffer_overflow;

//USART Receiver interrupt service routine interrupt [USART_RXC] void usart_rx_isr(void) {

char status;

status = UCSRA;

data = UDR;

if ((status & (FRAMING_ERROR|PARITY_ERROR|DATA_OVERRUN )) ==0) {

rx_buffer[rx_wr_index]=data;

if(++ rx_wr_index == RX_BUFFER_SIZE) rx_wr_index =0;

if(++ rx_counter == RX_BUFFER_SIZE) {

rx_counter = 0;

rx_buffer_overflow =1;

} } } //==================================================

//TRUYEN DATA //==================================================

//USART Transmitter buffer

#define TX_BUFFER_SIZE 8 char tx_buffer[TX_BUFFER_SIZE];

#if TX_BUFFER_SIZE <256 unsigned char tx_wr_index, tx_rd_index,tx_counter;

#else unsigned int tx_wr_index, tx_rd_index,tx_counter;

#endif //USART Transmitter interrupt service routine interrupt [USART_TXC] void usart_tx_isr(void) {

if (tx_counter) {

--tx_counter;

UDR = tx_buffer[tx_rd_index];

if (++tx_rd_index == TX_BUFFER_SIZE) tx_rd_index =0;

} }

#ifndef _DEBUG_TERMINAL_IO_

//Write a character to a USART Transmiter buffer

#define _ALTERNATE_PUTCHAR_

#pragma used+

void putchar (char c) {

while (tx_counter == TX_BUFFER_SIZE) #asm ("cli")

if (tx_counter || (( UCSRA & DATA_REGISTER_EMPTY) == 0)) {

tx_buffer[tx_wr_index]=c;

if(++ tx_wr_index == TX_BUFFER_SIZE) tx_wr_index =0;

++ tx_counter;

} else UDR =c;

delay_ms(20);

#asm("sei") }

#pragma used-

#endif //==================================================

Một phần của tài liệu Luận văn thạc sĩ Vật lý kỹ thuật: Thiết kế và chế tạo máy đo độ ồn dựa trên cảm biến âm microphone electret (Design and make a sound level meter by using a transducer electret microphone) (Trang 79 - 94)

Tải bản đầy đủ (PDF)

(113 trang)