PIC Vietnam

PIC Vietnam (http://www.picvietnam.com/forum/index.php)
-   Các ngôn ngữ lập trình khác (CCS C, HT PIC,...) (http://www.picvietnam.com/forum/forumdisplay.php?f=12)
-   -   Nguyễn Thế Vinh - căn bản lập trình CCS (http://www.picvietnam.com/forum/showthread.php?t=841)

hampic 25-10-2007 10:49 PM

1 Attachment(s)
Trích:

Nguyên văn bởi hpecom (Post 3250)
Mong anh chỉ bảo:
_ Nếu em muốn truyền một số thực từ máy tính xuống PIC qua RS232 thì có được không?
_ Nếu được mong anh cho em một ví dụ viết trên MS Comm của Visual Basic 6.0.
Cảm ơn anh!
Rất mong anh.
hpecom.

hehe, theo mình nghĩ thì truyền cái gì cũng được nhưng chú ý là rs232 mỗi lần truyền 1 byte cho nên, một số có giá trị lớn hơn 255 thì truyền coi chừng bị sai. Khi đó, bạn nên chuyển sang chuỗi và truyền rồi khi nhận thì ghép lại.
Viết bằng MS Comm trong VB thì dể mà, bạn muốn thử gửi 1 giá trị nào thì có thể tạo một editbox rồi lấy giá trị trong nó gán cho hàm Output. Nhận thì dùng cách bẩy sự kiện OnComm() với CommEvent = comEvReceive rồi nhận giá trị từ hàm Input.
Xem file đính kèm để hiểu rỏ hơn (có ví dụ).

Thân

LeDuc 16-11-2007 01:01 AM

Cho mình hỏi vậy nếu ta truyền một dữ liệu 8bit qua cổng nối tiếp thì làm thế nào vậy?
[code/]
#include <16F877.h>
#device *=16 adc=8
#fuses HS,NOWDT,NOPROTECT,NOLVP

#use delay(clock=20000000)
#use fast_io(a)
#use fast_io(d)
#use rs232(baud=19200,parity=n,xmit=pin_C6,rcv=pin_c7,b its=8)

int8 led[] ={0,1,2,3,4,5,6,7,8,9},k=0;
int8 so[4];
int8 quet[]={128,64,32,16};
int32 a,b,value;

#int_rtcc
void rtcc()
{
set_timer0(99);
b=a;
so[0]=a%10; a/=10;
so[1]=a%10;a/=10;
so[2]=a%10; a/=10;
so[3]=a; a=b;
portd=led[so[k]]+ quet[k]; k++;
if(k==4); k=0;
}

void main()
{
trisa=255;
trisd=0;
setup_adc(adc_clock_internal);
setup_adc_ports(all_analog);
set_adc_channel(0);
set-timer_0(rtcc_internal|rtcc_div_128);
enable_interrupts(int_rtcc);
enable_interrupts(global);
set_timer0(99);
while(true)
{
delay_ms(10);
value=read_adc();
a=value;
}
}
[/code]

Bây giờ để truyền dữ liệu qua cồng nối tiếp , mình phải làm sao đây ?
Bình thường phải tốn hết 8 chân VDK ở port d , nếu truyền qua cổng nối tiếp thì chỉ cần tốn có 2 chân con VDK hà . Rất mong các huynh chi giáo giúp đệ...
Thanks

tiendungkct 17-11-2007 10:35 AM

Bạn Thử Dung Giao Tiếp I2c Or Usart Xem Sao
Phấn Mền Thì Có Thể Tự Viết Bắng Vc++or Vb
Mình Cũng đang Làm Về Phần Này Hỵ Vọng Sẽ đươc Các Bạn Hỗ Trợ
Thanks

thuyvu 20-06-2008 05:55 AM

Trích:

Nguyên văn bởi NTVinh (Post 5160)
A: Để dễ hình dung, mình xin lấy một ví dụ mã giả như sau nhé. Cái mã giả này có nội dung là, bạn muốn nhận 32 số kiểu float về vi điều khiển (PIC6F877, AT89C51, DS89C420,...) từ một thiết bị đầu cuối nào đó thông qua cổng truyền thông RS232. Theo chuẩn RS232, và theo đặc điểm của các vi điều khiển đã liệt kê ở trên, ta thực hiên mã giả như sau.
- Khai báo một mảng 32 phần tử dạng float, bắt đầu từ địa chỉ 0x0010 trong ram.
float temp[32] _at_ 0x0010;
- Như vậy, tại địa chi 0x0010 sẽ đựng nôi dung của byte chứa bit dấu của số thực temp[0]..., tại địa chỉ 0x0014 đựng nội dung của byte chứa bit dấu của số thự temp[1]...
- Như vậy thì...ôi, thôi, viết dài quá!!! Bạn tự suy luận tiếp nhé.
Nguyễn Thế Vinh.

Bạn nào có mạch chuyển đổi RS485 sang RS232 thì gửi cho mình xin vơi:xuanvudk2@gmail.com

Hard 20-06-2008 10:26 AM

Hi,
Các bác cho tui hỏi cách nhảy đến Địa chỉ = (địa chỉ hiện tại + 2000) trong CCS bằng cách nào. Tui tìm trong Help mà không thấy cách nào để lấy địa chỉ con trỏ chương trình cả. Rất mong các bác chỉ giáo vụ này vì tui cũng chưa tiếp xúc với CCS nhiều.
Thân ái.

phucan30 04-09-2009 01:24 AM

có bác nào có tài liệu hướng dẫn sử dụng phần mền mplab IDE 8.02 ko?cho em đi.đây là địa chỉ mail của em nè:nguyenphucan19@yahoo.com,
các bác giúp giùm em nha,em thank nhiều

phucan30 04-09-2009 01:40 AM

ah.bac nao co tai lieu huong dan hoc lap trinh ccs thi cho em nha,em cam on nhieu,lan nay em quyet hoc cho ra tro ,cac bac lam on giup do em nha

12345X 13-09-2010 11:41 PM

anh vinh oi. a co tai lieu huong dan viet ccs c ko.
may cai ham cua ccs c do. chu a viet vay e cung ko piet vo ve ham no su dung lam sao nua, ne e doc ma ko hieu j nhieu lam. tai e moi hoc nen cung ko ranh lam. a thong cam nha. a giai thich dum e cong dung viet cua tung ham lun nha.

thanhks a
chuc a vui

dinhminh09 15-09-2010 08:27 PM

Trích:

Nguyên văn bởi phucan30 (Post 29289)
ah.bac nao co tai lieu huong dan hoc lap trinh ccs thi cho em nha,em cam on nhieu,lan nay em quyet hoc cho ra tro ,cac bac lam on giup do em nha


minh cho các bạn cái link cái này mình sưu tâm và gộp lại nói chung chỉ cần có bộ này là cũng đủ múa lửa về ccs c rôi
http://www.mediafire.com/?caus3w6276riwi7

linhdang 26-11-2010 11:14 PM

ai co bai tap[ c cho pic 16f887 ko cho minh xin voi minh cam on rat nhieu
email:hoanglinh_111988@yahoo.com

quangvanvo 08-01-2011 09:37 AM

trong mấy đoạn code thì thấy bạn sử dụng portx được còn mình thi nó báo lỗi hoài àh mình chỉ dùng set_tris_x(giá trị), set_port_x(giá trị) không à tại sao lại vậy áh có biết thì chỉ mình với you!!!!!!
nếu có thể giữ qua mail hoặc yahoo giùm mình nha:
<<phieudu_cunggio951@yahoo.com.vn>>

quangvanvo 11-01-2011 10:12 AM

các anh chị ơi em mới viết code cho pic lần đầu ma em đang muốn tìm hiểu về các ngắt mà đọc các tài liệu thì no không có các vidu rõ cho từng ngắt; thí dụ như ngắt timer1 khi nó đếm tràn thì xảy ra hiên tương ngắt tùi vào trong timer1 mình có cần nạp lai giá tri cho timer1 không hả các anh chị? các anh chị hãy bode len các ví dụ và có giai thích cho tuwng dòng giùm ưm với!!!!!!

hangocminh1989 10-07-2011 05:35 PM

Em làm đoạn code test cổng com với pic 16f887 như sau:
#include <16f887.h>
#include <def_887.h>
#device *=16adc=8
#use delay(clock=20M)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bi ts=8)//,stream=PORT1)
#include <lcd_lib_8bit.c>
//---------------------------
boolean flag_rb4=0;
//---------------------------
#INT_RB
void RB_isr(void)
{
if(!input(pin_b2))
{
flag_rb4=1;
clear_interrupt(INT_RB2);
}
}
#int_RDA
void RDA_isr(void)
{

}
void main()
{
lcd_init();
set_tris_c(0xf0);
lcd_putcmd(0xc0);
lcd_putchar("TEST");
setup_comparator(NC_NC_NC_NC);
enable_interrupts(INT_RDA);
enable_interrupts(INT_RB2);
enable_interrupts(GLOBAL);
while(1)
{
if(flag_rb4==1)
{
flag_rb4=0;
PRINTF("TEST");
lcd_putcmd(0x01);
lcd_putchar("DA TRUYEN");
}
}

}
khi em nhấn phím b4 thì truyền chuỗi từ pic lên terminal và lcd. nhưng sao trên terminal em không thấy cái dòng chữ nào hết, hic. bác nào biết giúp dỡ em chỗ này đi

chanhtho_1991 28-09-2012 12:08 PM

các anh ơi, em đang làm bài về mô phỏng các bài tập ví dụ trong thư viện của CCS4. em làm tới mô phỏng của 2 ví dụ này thì không biết làm sao nửa. các anh giúp em vẽ mạch mô phỏng trên protues với, hay là chỉ giúp em những linh kiện nào trong mạch, và cách kết nối sao với. em gửi ví dụ này các anh xem giúp và chỉ dẫn giúp em với, em chân thành cảm ơn

/////////////////////////////////////////////////////////////////////////
//// EX_CAN_CCS_A.C ////
//// ////
//// Example of CCS's CAN library, using the PIC18Fxx8. This ////
//// example was tested with and written for the CCS CAN Prototype ////
//// board. ////
//// ////
//// The CCS CAN Prototype board has four CAN nodes that communicate ////
//// to each other. Node A is the 18F458 with it's internal CAN ////
//// peripheral, Node B is a PIC16F87x connected to an external ////
//// MCP2510 CAN peripheral, and Node C and Node D are both MCP250xx ////
//// stand-alone CAN I/O expanders. This example is the firmware ////
//// for Node A. ////
//// ////
//// Every two seconds this firmware sends out a command to node B ////
//// to change it's leds (CAN ID 0x202) ////
//// ////
//// Upon change of the A/D reading, a value of 0-9 is sent to ////
//// Node D which is displayed on the 8-seg LCD (CAN ID 0x400) ////
//// ////
//// Pressing the Node A button sends a request to Node B (CAN ID ////
//// 0x201) for Node B's A/D reading, which Node B will respond ////
//// with a CAN message with it's A/D reading (with CAN ID 0x201). ////
//// Also, pressing the Node A button will change the LEDs on Node ////
//// C (CAN ID 0x300) ////
//// ////
//// Pressing Node C's buttons will cause Node A's buttons to change ////
//// (Node C transmits button changes with CAN ID 0x303) ////
//// ////
//// Using a serial port, you can examine all the CAN traffic as ////
//// seen by the 18xxx8. ////
//// ////
//// For more documentation on the CCS CAN library, see can-18xxx8.c ////
//// ////
//// Jumpers: ////
//// PCH pin C7 to RS232 RX, pin C6 to RS232 TX ////
//// ////
//// This example will work with the PCH compiler. ////
/////////////////////////////////////////////////////////////////////////
//// ////
//// Baud rate settings to use to connect to the CCS CAN Prototype ////
//// board at 20Mhz: ////
//// ////
//// Baud Rate Prescalar: 4 ////
//// Propagation Segment: 3xTq ////
//// Phase Segment 1: 6xTq ////
//// Phase Segment 2: 6xTq ////
//// Synchronized Jump Width: 1xTq ////
//// Sample Rate: 1x ////
//// Wakeup Filter: Off ////
//// ////
/////////////////////////////////////////////////////////////////////////
//// ////
//// Node C and D are seperate stand-alone MCP250xx CAN I/O ////
//// expanders. The CCS CAN Prototype board has these chips already ////
//// programmed correctly. However, if you wish to program your own ////
//// to work with this example, then use the provided .HEX files ////
//// a programmer capable of programming these chips. Or, make a ////
//// a new HEX file with these properties: ////
//// ////
//// NODE C: Set RX ID mask and buffers to receive ID 0x3**. (The ** ////
//// means make the least signifcant 8bits no-care in the mask). ////
//// Set TX1 buffer to ID 0x301, TX2 buffer to ID 0x302, TX3 buffer ////
//// to ID 0x303. Set GP0 to analog (and enable the A/D). Set GP1, ////
//// GP2 and GP3 to OUTPUT. Set GP4, GP5 and GP6 as INPUT with edge ////
//// trigger enable. Leave OPTREG2 clear, disable PWM1 and PWM2, ////
//// and disable scheduled transmission. Also, see the baud rate ////
//// settings above. ////
//// ////
//// NODE D: Set RX ID mask and buffers to receive ID 0x4**. (The ** ////
//// means make the least signifcant 8bits no-care in the mask). ////
//// Set TX1 buffer to ID 0x401, TX2 buffer to ID 0x402, TX3 buffer ////
//// to ID 0x403. Configure all ports as OUTPUT. Leave OPTREG2 ////
//// clear, disable PWM1 and PWM2, and disable scheduled ////
//// transmission. Also, see the baud rate settings above. ////
//// ////
/////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,2003 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS ////
//// C compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, ////
//// reproduction or distribution is permitted without written ////
//// permission. Derivative programs created using this software ////
//// in object code form are not restricted in any way. ////
/////////////////////////////////////////////////////////////////////////

#include <18F458.h>
#fuses HS,NOPROTECT,NOLVP,NOWDT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#define CAN_DO_DEBUG TRUE

#include <can-18xxx8.c>

#define PIN_LED1 PIN_A5
#define PIN_LED2 PIN_B5
#define PIN_LED3 PIN_B4

#define LED1_HIGH output_low(PIN_LED1)
#define LED1_LOW output_high(PIN_LED1)
#define LED2_HIGH output_low(PIN_LED2)
#define LED2_LOW output_high(PIN_LED2)
#define LED3_HIGH output_low(PIN_LED3)
#define LED3_LOW output_high(PIN_LED3)

#define BUTTON PIN_A4

#define BUTTON_PRESSED !input(BUTTON)

int16 ms;

const char lcd_seg[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x1 0}; //0 for on, 1 for off

#int_timer2
void isr_timer2(void) {
ms++; //keep a running timer that increments every milli-second
}

#define ASK_FOR_ID_AD_B 0x201 //ask for AD info from CAN port B
#define SET_LED_ID_B 0x202 //set LEDs for CAN port B
#define RESPOND_TO_LED_C_ID 0x303
#define WRITE_REGISTER_C_ID 0x300
#define WRITE_REGISTER_D_ID 0x400

void main() {
int b_leds=0;
int c_leds=1;
int a_leds=0;
struct rx_stat rxstat;
int32 rx_id;
int buffer[8];
int rx_len;

int last_lcd_output=0xFF;
int i,curr_lcd_output;

setup_port_a(RA0_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);

for(i=0;i<8;i++) {
buffer[i]=0;
}

LED1_HIGH;
LED2_HIGH;
LED3_HIGH;
printf("\r\n\r\nCCS CAN EXAMPLE\r\n");
delay_ms(1000);
LED1_LOW;
LED2_LOW;
LED3_LOW;

setup_timer_2(T2_DIV_BY_4,79,16); //setup up timer2 to interrupt every 1ms if using 20Mhz clock

can_init();

enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);

printf("\r\nRunning...");

while(TRUE)
{
if ( can_kbhit() )
{
printf("\r\n");
if(can_getd(rx_id, &buffer[0], rx_len, rxstat)) {
if (rx_id == ASK_FOR_ID_AD_B) {
printf("Channel B AD: %X\r\n",buffer[0]);
}
else if (rx_id == RESPOND_TO_LED_C_ID) { //node C is an mcp250x0 which sends out a message upon edge detection on IO
printf("Chaning LEDs\r\n"); //in_data[0]=iointfl, in_data[1]=gpio
a_leds=~(buffer[1]);
if (bit_test(a_leds,4)) {LED1_HIGH;} else {LED1_LOW;}
if (bit_test(a_leds,5)) {LED2_HIGH;} else {LED2_LOW;}
if (bit_test(a_leds,6)) {LED3_HIGH;} else {LED3_LOW;}
}
}
}

if ( can_tbe() && (ms > 2000)) //every two seconds, send new data if transmit buffer is empty
{
ms=0;

//change leds on port b
printf("\r\n\r\nSet LEDs on Port B to %U",b_leds);
can_putd(SET_LED_ID_B, &b_leds, 1, 1, 1, 0);
b_leds++;
if (b_leds > 7) {b_leds=0;}
}

if (BUTTON_PRESSED) {
while (BUTTON_PRESSED) {}
delay_ms(200);

//ask for AD on port B
printf("\r\n\r\nAsking for A/D reading on Port B...");
can_putd(ASK_FOR_ID_AD_B, 0, 1, 1, 1, 1);

//change LEDs on port C
buffer[0]=0x1E; //addr of gplat on 25050
buffer[1]=0x0E; //mask
buffer[2]=~(c_leds << 1); //new gplat values
printf("\r\nIncrementing LED on Port C");
can_putd(WRITE_REGISTER_C_ID, &buffer[0], 3, 1, 1, 0);
c_leds++;
if (c_leds > 7) {c_leds=0;}
}

//change lcd segment on port d
i=read_adc();
curr_lcd_output=i/26; //scale to 0-9
if (curr_lcd_output != last_lcd_output) {
last_lcd_output=curr_lcd_output;
printf("\r\nChanging 8-seg LCD on D to current A/D reading (%X, %X)",i,curr_lcd_output);
buffer[0]=0x1E; //addr of gplat
buffer[1]=0x7F; //mask
buffer[2]=lcd_seg[curr_lcd_output]; //new gplat values
can_putd(WRITE_REGISTER_D_ID, &buffer[0], 3, 1, 1, 0);
}
}
}


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

Tên diễn đàn: vBulletin Version 3.8.11
Được sáng lập bởi Đoàn Hiệp.
Copyright © PIC Vietnam