PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Các ngôn ngữ lập trình khác (CCS C, HT PIC,...)

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Tìm Kiếm Bài Trong Ngày Ðánh Dấu Ðã Ðọc Vi điều khiển

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 01-02-2016, 07:37 PM   #1
1041002
Đệ tử 3 túi
 
Tham gia ngày: Apr 2007
Bài gửi: 46
:
Tạo sóng sine với pic 18f45k22 và MCP4921

Trong project này, để tạo sóng sine ta sử dụng một DAC MCP4921 kết nối đến ngõ ra (output) của vi điều khiển 18f45k22 và một sóng sine được tạo ra trong thời gian thực ở ngõ ra của DAC đó với tần số 50 Hz, biên độ 2V và offset 2.5V.
Bạn có thể xem video tại đây: https://youtu.be/0rDgnJ63lsU

Sơ đồ nguyên lý:


Code:
Code:
/*
 *Project name: Tao song sin voi MCP4921
 *CopyRight:
             Written by: Dogan Ibrahim
            (c) dientudieukhien.net, 2015
 *Description:
            Trong project nay chung ta su dung vi dieu khien pic18f45k22 de dieu khien MCP4921 tao ra song sin
 *Configuration:
            Mivrocontroller: PIC 18f45k22
            Oscillator: HS, 08.000 MHz
            Device: kiem tra tren mach thu te, proteus mo phong khong chinh xac phan nay.
            Compiler: mikroC Pro for Pic ver 6.6.1
 *Website: dientudieukhien.net
*/
//DAC module connections
sbit Chip_Select at RC0_bit;
sbit Chip_Select_Direction at TRISC0_bit;
//End DAC module connection

#define T 100    //100 samples
#define R 0.0628 //2*PI/T
#define Amplitude 1638   //2V*4096/5V
#define offset 2048      //2.5*4096/5V

unsigned char temp, count=0;
float sample;
unsigned int value;
float sins[100];

// this funtion sends 12 bits digital data to the DAC. The data is passed
//throught the funtion argument called "value"

void DAC(unsigned int value)
{
char temp;
Chip_Select=0;    //Enable DAC chip

//sens hight byte
temp=(value>>8)& 0x0F;      //store bits 8:11 to temp
temp |=0x30;               //define DAC setting (choose 1xgain)
SPI1_Write(temp);          //send high byte via SPI

//send low byte
temp=value;                //store bits 0:7 to temp
SPI1_Write(temp);          //send low byte via SPI

Chip_Select=1;             //Disable DAC chip
}

//
//Timer ISR, program jumps here at very 200ms
//
void interrupt(void)
{
TMR0L=206;                 //reload timer register
//
//Get sine wave sample and send to DAC
//
value=sins[count];
DAC(value);                //send to DAC converter
count++;
if(count==100) count=0;
INTCON.TMR0IF=0;           //clear timer interrupt flag
}

void main()
{
unsigned char i;
ANSELC = 0;                //configure portc as digital
Chip_Select = 1;           //Disable DAC
Chip_Select_Direction = 0; //set CS as output
SPI1_Init();                //initialize SPI module
//
//Generate the sine wave samples offline and load into a arry called sins
//
for(i=0;i<100;i++)
sins[i]=offset+Amplitude*sin(R*i);
//
//configure TIMR0 interrupts
//
T0CON=0xC2;                 //TIMER0 in 8 bit mode
TMR0L=206;                  //Load timer regster
INTCON=0xA0;                 //Enable global and timer0 interrupts
for(;;)
{}
}
Bạn xem bài viết chi tiết cách tạo ra các thông số và hoạt động của MCP4921 tại đây
__________________


Projects for PIC, AVR in MikroC and C | Điện tử cơ bản | Mạch điện tử ứng dụng | Tài liệu điện tử | Tài liệu truyền hình | Ebook | Softwaves | tool online |


Visit my site address: http://dientudieukhien.net/
1041002 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 

Ðiều Chỉnh
Xếp Bài

Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 01:31 AM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam