We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
#include <t89c51ac2.h> //#include <math.h> void serialInterrupt (void); volatile char uart_data; char duty; void main(void) { // Set up UART mode SCON = 0x50; // uart in mode 1 (8 bit), REN=1 TMOD = TMOD | 0x20 ; // Timer 1 in mode 2 TH1 = 0xFB; // 9600 Bds at 18.432MHz TL1 = 0xFB; // 9600 Bds at 18.432MHz ES = 1; // Enable serial interrupt EA = 1; // Enable global interrupt */ TR1 = 1; // Set up PWM mode CMOD = 0x02; // Setup PCA timer CL = 0x00; CH = 0x00; CCAP0L = 0x40; // Set the initial value same as CCAP0H CCAP0H = 0x40; // 75% Duty Cycle CCAPM0 = 0x42; // Setup PCA module 0 in PWM mode. CR = 1; // Start PCA Timer.*/ // Init variable uart_data = 128; // 50% Duty Cycle //Program main routime, do forever while (1) { P2 = 0xFF; // turn off all lamps (for test) P2_2=0; // Turn on CCAP0H = (int) uart_data; } } void serialInterrupt (void) interrupt 4 using 1{ if (RI) { // Byte received on UART. uart_data = SBUF; if (uart_data == '1') // For Testing P2_0 = 0; SBUF = uart_data; // Initiate transmit of the byte received. RI = 0; // Clear interrupt flag. } else if (TI) { // Finished transmitting a byte. //P2_4 = 0; TI = 0; } }
I try to control position of motor. So I need receive new position from computer through RS232 and control motor by PWM mode 1.
My PWM code (without UART) controlling a motor. Result: SUCCESS!
My UART code (without PWM) echoing characters it receives. Result: SUCCESS!
I have 2 questions:
1. Why in UART code, in serialInterrupt function I have to put this code:
if (uart_data == '1') // For Testing P2_0 = 0;
and in main loop :
P2 = 0xFF; // turn off all LED (for test) P2_2=0; // Turn on
If I use this code (first time I only think it only use for test) UART is well done, but when I close it, UART can not work correct.
2. If I combine UART and PWM projects, the new project (above code) only has PWM but can not send/receive.
One body said that: "If that indicates a PCA interrupt (I don't know because I'm not familiar with how uVision debugger presents the PCA subsystem) and you don't have an ISR for it, then that could cause a lot of problems."
But I don't understand much.
Did I miss somethings in my code?
Kind Regards, Mr. Huy