|
|
View previous topic :: View next topic |
Author |
Message |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 15, 2010 3:15 pm |
|
|
I got tired of this so I made it work in hardware.
First I made a program to create the two test waveforms. This program
runs on the first PicDem2-Plus board:
Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#define WAVEFORM_0 PIN_B0
#define WAVEFORM_1 PIN_B1
//==========================================
void main()
{
output_low(WAVEFORM_0);
output_low(WAVEFORM_1);
delay_ms(100);
while(1)
{
output_high(WAVEFORM_0);
delay_us(448);
output_high(WAVEFORM_1);
delay_ms(40);
output_low(WAVEFORM_0);
delay_us(448);
output_low(WAVEFORM_1);
delay_ms(40);
}
} |
Then I took one of your original programs, and modified it to use RS-232
output instead of the LCD, and I put it on the 2nd PicDem2-Plus board:
Code: |
#include <16F877.h>
#FUSES NOWDT
#FUSES XT
#FUSES PUT
#FUSES NOPROTECT
#FUSES NODEBUG
#FUSES NOLVP
#FUSES NOCPD
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
long rise,fall,pulse_width;
#int_ccp2
void isr()
{
rise = CCP_1;
fall = CCP_2;
pulse_width = fall - rise;
}
void main()
{
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
setup_ccp2(CCP_CAPTURE_RE); // Configure CCP2 to capture fall
setup_timer_1(T1_INTERNAL); // Start timer 1
enable_interrupts(INT_CCP2); // Setup interrupt on falling edge
enable_interrupts(GLOBAL);
while(TRUE) {
delay_ms(1000);
printf("%lu us \n\r", pulse_width );
}
} |
Then I connected the two boards together, with jumper wires.
Pin B0 on board 1 goes to pin C2 on Board 2.
Pin B1 on board 1 goes to pin C1 on Board 2.
Ground on board 1 goes to ground on Board 2.
Then I loaded TeraTerm and ran both boards and I got the following
output, which is in the expected range. All of these was done in less
than 10 minutes. It was compiled with vs. 4.057. I mean, days are
spent with the simulator, but two boards and an oscilloscope works a
lot better.
Quote: |
452 us
452 us
452 us
452 us
452 us
452 us
452 us
452 us
452 us
452 us
452 us |
|
|
|
matheuslps
Joined: 29 Sep 2010 Posts: 73 Location: Brazil
|
|
Posted: Mon Nov 29, 2010 4:10 am |
|
|
Thanks for your reply. The project is almost done. I have just more 1 question.
I just found the math library and want to know if I am using this right. I read that the sine and cosine functions are in radians.. so a made the conversion.
I am using a fake usb to connect the pic to the hyperterminal..... so a need to know if is necessary declare the RS232 protocol...
The code @ PCW 4.057
Code: |
#include <18F4550.h> //The PIC
#device adc=10 //ADC 10bits
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN //Fuses
#use delay(clock=48000000) //Clock
#include <Math.h> //To make some calcs
#define USB_CON_SENSE_PIN PIN_B2 //Yes!
#include <usb_cdc.h>
void main()
{
long valor_1, valor_2, diferenca; //Variables
long tension_1=0, tension_2=0;
float envio, angulo_degree, angulo_radian, tensao_onda_1, tensao_onda_2, pot_ativa, pot_reativa;
int16 adc_1, adc_2=0;
usb_cdc_init();
usb_init();
setup_adc_ports(AN0_TO_AN1);
setup_adc(ADC_CLOCK_DIV_64);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
for(;;)
{
set_adc_channel(0);
delay_us(20);
adc_1=read_adc();
if (adc_1 > tension_1) //To find the biggest value
{
valor_1 = get_timer1();
tension_1 = adc_1;
}
set_adc_channel(1);
delay_us(20);
adc_2=read_adc();
if (adc_2 > tension_2) //To find the biggest value
{
valor_2 = get_timer1();
tension_2 = adc_2;
}
diferenca = (valor_2 - valor_1);
if (valor_2 < valor_1)
{
diferenca+=65536;
}
diferenca/=12; //Time in microseconds
envio = (diferenca/1000); //Time in miliseconds
usb_task();
if (usb_enumerated())
{
angulo_degree = (360*60*envio)/1000; //angle in degrees
angulo_radian = (angulo_degree*PI)/180; //angle in radians
tensao_onda_1 = (5*tension_1)/1023; //max value 1
tensao_onda_2 = (5*tension_2)/1023; //max value 2
pot_ativa = (((5*tension_1)/1023)*((5*tension_2)/1023)*(cos(angulo_radian)))/2; //Active power ?
pot_reativa = (((5*tension_1)/1023)*((5*tension_2)/1023)*(sin(angulo_radian)))/2; //reactive power ?
delay_ms(500);
printf(usb_cdc_putc,"\f T= %1.2f ms Tensao Entrada= %2.3f V Tensao Saida= %1.2f V Pot. Ativa= %1.2f W Pot. Reativa= %1.2f VAR" , envio,tensao_onda_1,tensao_onda_2,pot_ativa,pot_reativa); //show
}
}
} |
thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 29, 2010 12:45 pm |
|
|
Are you using a USB to serial cable like this:
http://www.siig.com/ViewProduct.aspx?pn=JU-CB1S12-S3
If so, the USB end of the cable needs to plug into a USB master, such as
the PC. The 18F4550 can only be a USB slave device. You can't plug
the USB end of the cable into the 18F4550 board.
You can plug the USB-to-serial cable into the PC, and the plug the serial
end of it into a PIC board (with a Max232-type chip on it). But of course
if you are using serial to talk to a PIC, you must use the #use rs232()
statement to define the serial port on the PIC. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Nov 29, 2010 12:52 pm |
|
|
temtronic wrote: | rule number 1: Simulation is NOT the real world
rule number 2: NEVER trust the simulator program, unless YOU wrote it.
|
Rule #3: Even if you wrote it - are you sure you can trust it?
Honestly, the simulator stuff should become a sticky at the top of the forum thread list with something like a list of "10 Commandments of Simulators."
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
matheuslps
Joined: 29 Sep 2010 Posts: 73 Location: Brazil
|
|
Posted: Mon Dec 06, 2010 9:25 am |
|
|
hi, it is me again.
So, my project worked like a charm on a breadboard.
_BUT_ I needed come back here and tell that the problem that I was having on the simulator was not a simulator issue.
Look at the image below and pay attention on the two red retangles on the 18F4550:
http://img600.imageshack.us/img600/6268/semttulomd.png
The dunbass here, conected the second signal at the PIN B3, the second CCP2! That is because the code was not working. No interrupt was happening.....
The correct PIN is RC1!.
Now, the program works fine on the simulation too.
Thanks! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|