#include <pic.h>//configuration fuses__CONFIG(XT & WDTDIS & PWRTDIS & UNPROTECT);void delay(void) //this is a delay function{unsigned int i;for(i=0;i<0xFFFF;i++) //FOR loop does nothing, just; //increment variable i from 0} //to 65535void main (){TRISB = 0b00001111; //RB7-4 are output pinsif (TRISB2 = 0){PORTB = 0b10100000; //forward}else{PORTB = 0b00010000; //turn left}if (TRISB3 = 0){PORTB = 0b10100000; //forward}else{PORTB = 0b01000000; //turn right}if (TRISB1 = 0 ){PORTB = 0b10010000; //search}else{PORTB = 0b10100000; //forward}while(1) //infinite loop{delay(); //invoke delay() function}}
heto ang gamit ko, macro tsaka bit operators and shiftspwm.hCode: [Select]//********************* MODIFY HERE *****************************#ifndef FOSC_FREQ#define FOSC_FREQ 20MHZ /* Crystal frequency in MHz */#endif#define MHZ *1000000L //dont modify #define KHZ *1000L //dont modify #define PWM_MODE 0x0C //00001100#define PWM_FREQ 100KHZ //khz #define TMR2_PRES 1 //prescaler = 1,4,16#define DUTY_CYCLE 50 //duty cycle in %//****************************************************************#define PR2_VALUE ((FOSC_FREQ / (PWM_FREQ * 4 * TMR2_PRES)) - 1)#define PWM_DC_FREQ ((PWM_FREQ * 100) / DUTY_CYCLE)#define _10BIT_DUTY_CYCLE (FOSC_FREQ / (PWM_DC_FREQ * TMR2_PRES))void init_PWM(void);pwm.cCode: [Select]void init_PWM(void){ //clear TMR2 relevant registers CCP1CON = 0x00; CCPR1L = 0x00; TMR2 = 0x00; //clear TMR2 register //T2CON = 0; //TMR2 is off //assign prescaler switch(TMR2_PRES){ case 1: T2CON &= ~0x03; break; case 4: T2CON |= 0x01; break; case 16: T2CON |= 0x02; break; } //configure TMR2 in PWM mode CCP1CON |= PWM_MODE; //PWM mode PR2 = PR2_VALUE; //set PWM frequency //assign duty cycle CCPR1L = (_10BIT_DUTY_CYCLE >> 2); CCP1CON |= (_10BIT_DUTY_CYCLE << 4) & 0x30; //PWM output pin TRISC2 = 0; //disable interrupts PEIE = 0; GIE = 0; //enable PWM TMR2ON = 1; }
//********************* MODIFY HERE *****************************#ifndef FOSC_FREQ#define FOSC_FREQ 20MHZ /* Crystal frequency in MHz */#endif#define MHZ *1000000L //dont modify #define KHZ *1000L //dont modify #define PWM_MODE 0x0C //00001100#define PWM_FREQ 100KHZ //khz #define TMR2_PRES 1 //prescaler = 1,4,16#define DUTY_CYCLE 50 //duty cycle in %//****************************************************************#define PR2_VALUE ((FOSC_FREQ / (PWM_FREQ * 4 * TMR2_PRES)) - 1)#define PWM_DC_FREQ ((PWM_FREQ * 100) / DUTY_CYCLE)#define _10BIT_DUTY_CYCLE (FOSC_FREQ / (PWM_DC_FREQ * TMR2_PRES))void init_PWM(void);
void init_PWM(void){ //clear TMR2 relevant registers CCP1CON = 0x00; CCPR1L = 0x00; TMR2 = 0x00; //clear TMR2 register //T2CON = 0; //TMR2 is off //assign prescaler switch(TMR2_PRES){ case 1: T2CON &= ~0x03; break; case 4: T2CON |= 0x01; break; case 16: T2CON |= 0x02; break; } //configure TMR2 in PWM mode CCP1CON |= PWM_MODE; //PWM mode PR2 = PR2_VALUE; //set PWM frequency //assign duty cycle CCPR1L = (_10BIT_DUTY_CYCLE >> 2); CCP1CON |= (_10BIT_DUTY_CYCLE << 4) & 0x30; //PWM output pin TRISC2 = 0; //disable interrupts PEIE = 0; GIE = 0; //enable PWM TMR2ON = 1; }
mga master, hindi ko tlga makuha sa PIC to PIC Tx to Rx, transfer po sana ako ng 1word character.. baka meron po kayo smple program in mikroC po baka pwd po makahingi, kahit ung pinaka simple lang po, basta matransmit niya ung word then e display ng receiver PIC sa LCD, nahihirapan po ako mag program ng uart, paki check na lang po if may mali sa mga connections ko po.. tnx po tlg, ..eto po ung image
unsigned char _data = 0x41; unsigned char _data1 = 0x42; unsigned char _data2 = 0x43; unsigned char _data3 = 0x44;void main(){ UART1_Init(9600); // Initialize UART module at 9600 bps delay_ms (2000); do { UART1_Write(_data); delay_ms(1000); UART1_Write(_data1); delay_ms(1000); UART1_Write(_data2); delay_ms(1000); UART1_Write(_data3); delay_ms(1000); } while (1);}
// LCD module connectionssbit LCD_RS at RB4_bit;sbit LCD_EN at RB5_bit;sbit LCD_D4 at RB0_bit;sbit LCD_D5 at RB1_bit;sbit LCD_D6 at RB2_bit;sbit LCD_D7 at RB3_bit;sbit LCD_RS_Direction at TRISB4_bit;sbit LCD_EN_Direction at TRISB5_bit;sbit LCD_D4_Direction at TRISB0_bit;sbit LCD_D5_Direction at TRISB1_bit;sbit LCD_D6_Direction at TRISB2_bit;sbit LCD_D7_Direction at TRISB3_bit;// End LCD module connectionsunsigned char uart_rd;unsigned char temp;void main(){ Lcd_Init(); // Initialize LCD Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off UART1_Init(9600); // Initialize UART module at 9600 bps delay_ms (1000); do { if (UART1_Data_Ready() == 1) { uart_rd = UART1_Read(); UART1_Write(uart_rd); temp = uart_rd; } Delay_ms(100); Lcd_Chr(1,2, temp); } while (1);}