View
226
Download
0
Category
Preview:
Citation preview
8/4/2019 Can Ban Gtmt Voi Vdk Avr
1/17
hocdelam.org
Giao ti p my tnh vi vi iu khi n AVR. Th hai, 27 Thng 4 2009 07:13 lehoanganh - Hocdelam Group .
Tm t t:
Bi vit ny nh m mc ch hng dn cc bn mi lm quen vi iu khin AVR. Tc gi ch n phngphp s dng h nh nh km theo s ch thch trc tip to nn tnh tr c quan, sinh ng, hng dny nhm gip cho cc bn sp t m hi u hoc ang t m hi u v vn ny c th t m th y s hu cht bi vit. Bi vit ny ch hng dn phn cc phn cn bn nht. Mong rng n s l n n tng gip ccbn c qua c th t lm cc Project ph c tp hn. Ni dung bi vit chia lm 2 ph n chnh:
Ph n 1- Hng dn cch to 1 bng iu khin n gin truyn nhn d liu dng VisualStudio C++ 2005.Ph n 2- Dng ph n mm CodeVisionAVR C Compiler vit code truyn nhn d liu cho VK.
Chu n b:
Ph n cng: Kit th nghim Atmega32 + Mch np + Cp cng COM.Ph n mm s dng:
Visual Studio.NET 2005.CodeVisionAVR C Compiler
Chng tr nh n p AVR Prog.
Thi gian thc hin: 30 pht
Ni dung:
Thit k 1 bng iu khin n gin truyn nhn d liu t PC v VK dng Visual Studio 2005(hoc VB6.0). Dng ph n mm CodeVisionAVR C Compiler vit code truyn nhn d liu cho VK.
ng dng :
S dng h tr mt phn trong cc ti tt nghip, n, tiu lun, ngh nghip trong tng lai ...
Kt qu:
Hnh 1: y l giao din truyn nhn d liu sau khi l m xong.
8/4/2019 Can Ban Gtmt Voi Vdk Avr
2/17
hocdelam.org
Cch th c hin nh sau: Ph n 1- Dng ph n mm CodeVisionAVR C Compiler vit code truyn nhn d liu cho VK.
Hnh 2: T o Pro ject m i trong CodeVisionAVR C Compiler
Hnh 3: Ch n Chip v giao ti p USART.
8/4/2019 Can Ban Gtmt Voi Vdk Avr
3/17
hocdelam.org
Hnh 4: Ch n cu h nh input, output v sinh code.PORTD.0 = In, PORTD.1 = Out, PORTC = Out
Hnh 5: Vi t code thc hin vic truyn nhn d liu.
8/4/2019 Can Ban Gtmt Voi Vdk Avr
4/17
hocdelam.org
Ph n 2- Hng dn cch to 1 bng iu khin n gin truyn nhn d liu d ng Visual StudioC++ 2005.
Hnh 6: M chng tr nh Visual Studio C++ 2005 t o Project mi.
Hnh 7: Ch n lp tr nh Windows Forms Application, t tn v ch n ni lu Project
8/4/2019 Can Ban Gtmt Voi Vdk Avr
5/17
hocdelam.org
Hnh 8: Cch m cc ca s Solution Explorer, Toolbox, Error List.
Hnh 9: Cc c a s cn ch Solution Explorer, Toolbox, Properties, Error List.
8/4/2019 Can Ban Gtmt Voi Vdk Avr
6/17
hocdelam.org
Hnh 10: M cc tag Solution Explorer, Propertie s ( thay i thuc tnh cc component).
Hnh 11: L y cc component t thanh Toolbox.
8/4/2019 Can Ban Gtmt Voi Vdk Avr
7/17
hocdelam.org
Hnh 12 : t tn v vi t Text cho cc Button trong thanh Properties.
Hnh 13: Vi t code cho cc Button.
8/4/2019 Can Ban Gtmt Voi Vdk Avr
8/17
hocdelam.org
Hnh 14: Built v Debug chng tr nh.
8/4/2019 Can Ban Gtmt Voi Vdk Avr
9/17
8/4/2019 Can Ban Gtmt Voi Vdk Avr
10/17
hocdelam.org unsigned char rx_wr_index,rx_rd_index,rx_counter;#elseunsigned int rx_wr_index,rx_rd_index,rx_counter;#endif
// This flag is set on USART Receiver buffer overflowbit rx_buffer_overflow;
// USART Receiver interrupt service routineinterrupt [USART_RXC] void usart_rx_isr(void){char status,data;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;};
};}
#ifndef _DEBUG_TERMINAL_IO_ // Get a character from the USART Receiver buffer#define _ALTERNATE_GETCHAR_ #pragma used+char getchar(void){char data;
while (rx_counter==0);data=rx_buffer[rx_rd_index];if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;#asm("cli")--rx_counter;#asm("sei")return data;}#pragma used-#endif
// USART Transmitter buffer#define TX_BUFFER_SIZE 128
char tx_buffer[TX_BUFFER_SIZE];#if TX_BUFFER_SIZE
8/4/2019 Can Ban Gtmt Voi Vdk Avr
11/17
hocdelam.org {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 the USART Transmitter 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;}
elseUDR=c;
#asm("sei")}#pragma used-#endif
// Standard Input/Output functions#include
// Declare your global variables here
void main(void){
// 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=TPORTA=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=TPORTB=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=TPORTC=0xFF;
8/4/2019 Can Ban Gtmt Voi Vdk Avr
12/17
hocdelam.org DDRC=0xFF;
// Port D initialization // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=In // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=TPORTD=0x00;DDRD=0xFE;
// Timer/Counter 0 initialization // Clock source: System Clock // Clock value: Timer 0 Stopped // Mode: Normal top=FFh // OC0 output: DisconnectedTCCR0=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: OffTCCR1A=0x00;TCCR1B=0x00;TCNT1H=0x00;TCNT1L=0x00;
ICR1H=0x00;ICR1L=0x00;OCR1AH=0x00;OCR1AL=0x00;OCR1BH=0x00;OCR1BL=0x00;
// Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer 2 Stopped // Mode: Normal top=FFh // OC2 output: DisconnectedASSR=0x00;
TCCR2=0x00;TCNT2=0x00;OCR2=0x00;
// External Interrupt(s) initialization // INT0: Off // INT1: Off // INT2: OffMCUCR=0x00;MCUCSR=0x00;
8/4/2019 Can Ban Gtmt Voi Vdk Avr
13/17
hocdelam.org
// Timer(s)/Counter(s) Interrupt(s) initializationTIMSK=0x00;
// USART initialization // Communication Parameters: 8 Data, 1 Stop, No Parity // USART Receiver: On // USART Transmitter: On // USART Mode: Asynchronous // USART Baud rate: 9600UCSRA=0x00;UCSRB=0xD8;UCSRC=0x86;UBRRH=0x00;UBRRL=0x67;
// Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: OffACSR=0x80;SFIOR=0x00;
// Global enable interrupts#asm("sei")
while (1){k=getchar();switch(k){case 'a':
PORTC=0B00001111;putchar('b');
break;case 'b':PORTC=0B11110000;putchar('c');break;
case 'c':PORTC=0B01010101;putchar('d');break;
case 'd':PORTC=0B00000000;putchar('e');break;
case 'e':PORTC=0B11111111;putchar('f');break;
default:putchar(k);
};}}
8/4/2019 Can Ban Gtmt Voi Vdk Avr
14/17
hocdelam.org
Code Giao di n truyn nhn:
#pragma once
namespace TestStringC {
using namespace System;using namespace System::ComponentModel;using namespace System::Collections;using namespace System::Windows::Forms;using namespace System::Data;using namespace System::Drawing;
/// /// Summary for Form1////// WARNING: If you change the name of this class, you will need to
change the
/// 'Resource File Name' property for the managed resourcecompiler tool
/// associated with all .resx files this class depends on.Otherwise,
/// the designers will not be able to interact properly withlocalized
/// resources associated with this form./// public ref class Form1 : public System::Windows::Forms::Form{public :
Form1( void ){
InitializeComponent();////TODO: Add the constructor code here//
}
protected :/// /// Clean up any resources being used./// ~Form1(){
if (components){
delete components;}
}private : System::IO::Ports::SerialPort^ sp1;private : System::Windows::Forms::TextBox^ txtt;private : System::Windows::Forms::Button^ btnt;private : System::Windows::Forms::TextBox^ txtr;private : System::Windows::Forms::Button^ btnr;private : System::Windows::Forms::Button^ btnc;protected :
8/4/2019 Can Ban Gtmt Voi Vdk Avr
15/17
hocdelam.org private : System::ComponentModel::IContainer^ components;
private :/// /// Required designer variable./// String^s; // khai bao chuoi sString^a; // khai bao chuoi a
#pragma region Windows Form Designer generated code/// /// Required method for Designer support - do not modify/// the contents of this method with the code editor./// void InitializeComponent( void ){
this ->components = ( gcnew System::ComponentModel::Container());
this ->sp1 = ( gcnew System::IO::Ports::SerialPort( this ->components));
this ->txtt = ( gcnew System::Windows::Forms::TextBox());this ->btnt = ( gcnew System::Windows::Forms::Button());this ->txtr = ( gcnew System::Windows::Forms::TextBox());this ->btnr = ( gcnew System::Windows::Forms::Button());this ->btnc = ( gcnew System::Windows::Forms::Button());this ->SuspendLayout();//// txtt//this ->txtt->Location = System::Drawing::Point(18, 25);this ->txtt->Name = L "txtt" ;this ->txtt->Size = System::Drawing::Size(73, 20);this ->txtt->TabIndex = 0;//// btnt//this ->btnt->Location = System::Drawing::Point(125, 25);this ->btnt->Name = L "btnt" ;this ->btnt->Size = System::Drawing::Size(75, 23);this ->btnt->TabIndex = 1;this ->btnt->Text = L "Transmit" ;this ->btnt->UseVisualStyleBackColor = true ;this ->btnt->Click += gcnew System::EventHandler( this ,
&Form1::btnt_Click);//
// txtr//this ->txtr->Location = System::Drawing::Point(18, 82);this ->txtr->Name = L "txtr" ;this ->txtr->Size = System::Drawing::Size(73, 20);this ->txtr->TabIndex = 0;//// btnr//this ->btnr->Location = System::Drawing::Point(125, 80);
8/4/2019 Can Ban Gtmt Voi Vdk Avr
16/17
hocdelam.org this ->btnr->Name = L "btnr" ;this ->btnr->Size = System::Drawing::Size(75, 23);this ->btnr->TabIndex = 1;this ->btnr->Text = L "Receive" ;this ->btnr->UseVisualStyleBackColor = true ;this ->btnr->Click += gcnew System::EventHandler( this ,
&Form1::btnr_Click);//// btnc//this ->btnc->Location = System::Drawing::Point(77, 133);this ->btnc->Name = L "btnc" ;this ->btnc->Size = System::Drawing::Size(75, 23);this ->btnc->TabIndex = 1;this ->btnc->Text = L "Clear" ;this ->btnc->UseVisualStyleBackColor = true ;this ->btnc->Click += gcnew System::EventHandler( this ,
&Form1::btnc_Click);//// Form1
//this ->AutoScaleDimensions = System::Drawing::SizeF(6, 13);this ->AutoScaleMode =
System::Windows::Forms::AutoScaleMode::Font;this ->ClientSize = System::Drawing::Size(226, 175);this ->Controls->Add( this ->btnc);this ->Controls->Add( this ->btnr);this ->Controls->Add( this ->txtr);this ->Controls->Add( this ->btnt);this ->Controls->Add( this ->txtt);this ->Name = L "Form1" ;this ->Text = L "Form1" ;this ->Load += gcnew System::EventHandler( this ,
&Form1::Form1_Load);this ->ResumeLayout( false );this ->PerformLayout();
}#pragma endregionprivate : System::Void btnt_Click(System::Object^ sender, System::EventArgs^e) {
s=txtt->Text; // gan chuoi tu textbox txtt va ssp1->WriteLine(s); // gui chuoi s xuong VDK
}
private : System::Void btnr_Click(System::Object^ sender, System::EventArgs^
e) {
a=sp1->ReadLine(); // nhan chuoi tu VDK gui len demgan vao chuoi a
txtr->Text=a; // gui chuoi a len textbox txtr
}private : System::Void btnc_Click(System::Object^ sender, System::EventArgs^e) {
8/4/2019 Can Ban Gtmt Voi Vdk Avr
17/17
hocdelam.org txtr->Text= "" ; // chuoi rongtxtt->Text= "" ;
}private : System::Void Form1_Load(System::Object^ sender, System::EventArgs^e) {
sp1->Open(); // mo cong ngay khi Form1 vua Debug}
};}
Recommended