Archives
-
[PIC16F616] Setting the ports to Analog/Digital in
What I want to do is, make a port on the MCU high, and if the MCU detects that the pin is high, it should output a high signal on another pin.
The pin I want to use as digital in is RC2, and I want to use RC4 as digital out.
This is the code I have written so far, but it isn’t working yet, could someone please tell me what I am doing wrong?code:
__CONFIG (BORDIS amp OSC_4MHZ amp UNPROTECT amp MCLREN amp PWRTDIS amp WDTDIS amp INTCLK);
#define _XTAL_FREQ (4000000L)
#define toggleDelay 500
#define toggleInterval 5volatile int counter=0;
void main()
{
INTCON = 0b10100100; //bit 7 GIE: Global Interrupt Enable bit
//bit 6 PEIE: Peripheral Interrupt Enable bit
//bit 5 T0IE: Timer0 Overflow Interrupt Enable bit
//bit 4 INTE: RA2/INT External Interrupt Enable bit
//bit 3 RAIE: PORTA Change Interrupt Enable bit(1)
//bit 2 T0IF: Timer0 Overflow Interrupt Flag bit(2)
//bit 1 INTF: RA2/INT External Interrupt Flag bit
//bit 0 RAIF: PORTA Change Interrupt Flag bitOPTION = 0b11000111; //bit 7 RAPU: PORTA Pull-up Enable bit
//bit 6 INTEDG: Interrupt Edge Select bit
//bit 5 T0CS: Timer0 Clock Source Select bit
//bit 4 T0SE: Timer0 Source Edge Select bit
//bit 3 PSA: Prescaler Assignment bit
//bit 2-0 PSlt2:0gt: Prescaler Rate Select bitsTRISA = 0b00011000; //Define PORTA, 0 == Input, 1 == Output
TRISC = 0b00001110; //Define PORTC
PORTA = 0b00000000; //Clear PORTA, 0 == output low
PORTC = 0b00000000; //Clear PORTC
ANSEL = 0b00001000; //Disable A/D, 0 == digital, 1 == analogint cycle_count = toggleInterval / 0.065536;
while(1)
{
if(RC2== 1) RC4 = 1;}
}void interrupt t0isr(void)
{
if (T0IF==1) {
//counter++;
T0IF=0;
return ;
}
// process other interrupt sources here
}Related Forum Messages
- PIC16F616 Interrupt
- 1 second interrupt to blink a LED!!??
- Interrupt-based servo control not working
- Servo control with PIC16
- Soft UART problems – PIC 16F628
- PIC18F45K22 INT0
- RB port change interrupt need some help
- Variable Pulse with 50Hz Duty Cycle
- Servo & Timer0
- (Mis)calculating the TMR0 overflow period?
- PORTB Interrupt On Change (IOC) problem
- [Timer] System LED Question
- Interrupt handling using pic16f877a
- 16F877 Timer0 Interrupt Affecting ADC
- Interrupt programming doubt
- Need help on pic16f690 stopwatch debug
- How TMR0 value decrement/increment before TMR0 overflow
- Inteerupt programming doubt
- PIC16F877A coding
- HowTo use Timer1 for interrupt
Besides the fact this is my first post on the microchip forums, I also have a question regarding to the PIC16F616. For some time I have been trying to get an interrupt working to just toggle a pin, but I can't get this to work.. I will post my code, and maybe someone can help me or give me some hints. Your help is greatly appreciated. Code: __CONFIG (BORDIS amp OSC_4MHZ amp UNPROTECT amp MCLREN amp PWRTDIS amp WDTDIS amp INTCLK); #define _XTAL_FREQ (4000000L) #define TRUE 1 void main() { ...Hello.I tested the following code using an interrupt to blink a LED in every 1 second. But,that does'nt work and does'nt blink.!!?? Would you please tell me what is wrong with the following code? Thanks, Code: Select all /* Here are the code and my hardware specifications; [ PIC18F452,XTAL=20MHZ,mikroC PRO for PICv5.6.1 ]. */ unsigned char counter=0;//Overflow counter. void interrupt(){ //Main Interrupt Service Routine(ISR). if(INTCON.TMR0IE ampamp INTCON.TMR0IF){ //Check if it is TMR0 Overflow ISR. ...Hi there. This is a servo code originally from ermicro's blog. I modified it so that the pulse values can be increased and decreased for a max of 25(2.5ms) and min of 5(.5ms). I'm using push buttons to as the two increase/decrease inputs. I can see the interrupt and main loop working as the LEDs got toggled all the time, but when I pressed the buttons the servo didn't run at all. Here's the code: Code : //__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT \ // & UNPROTECT & BORDIS & IESODIS & FCMDIS); // Using Internal Clock of 8 ...I am new to servos and purchased a Hitec HS-81 servo for my range finding robot. I built a small test circuit with a PIC16F690, servo, and a switch. The idea being that every time I press the switch the servo increments to another position. Nothing happens, however, when I turn on voltage. I believe my timers may be wrong but I checked many times. Anyone have any suggestions? (sorry for comments wrap) Code: #include <htc.h> __CONFIG (INTIO & WDTDIS & MCLRDIS & UNPROTECT); int tick = 0; void main(void) { ANSEL = 0; ANSELH = 0; CM1CON0 = 0; CM2CON0 = 0; TRISC1 = 1; //RC1 input TRISC2 = 0; //RC2 ...Hi, I've been struggeling for days making the software uart work. I've attached the schematic and the sourcecode is outlined below. I'm hoping that someone will help make this work. I've been meassuring pin RB1 with a scope, but it seems dead when sending data. If i do a simple loop: PORTB.B1 = PORTB.B1?0:1; i can see, that the pin is working ... The PIC seems healthy. I've tried with normal/inverted ports, Pull-ups on and off ... nothing seems to do the trick Also I'm not able to read data from pin RB0. When sending data from a PC UART Terminal I'm able to meassure the signal ...I don't know why the INT0 is not working for me. In am generating a PWM frequency on CCP2, and I connected the output (RC1) to INT0 (RB0), and I don't get the interrupt... Can someone please take a look to see if I'm doing anything wrong? I'm running the PIC at 64MHz using the 16MHz internal oscillator with 4x PPL. I also configured the TMR0 to see if the interrupt is called, and the timer is working fine, but the INT0 is not working. Code: Select all #define LED LATB.F5 void Config(void) { OSCTUNE = 0b01000000; OSCCON = 0b01110000; ...After I checked out datasheet for 16F887 (on EasyPIC5 devboard) I played with various functions so I finally got to RB port change interrupts. I think I initialised all the registers correctly (BTW- Mince's nice template was a good starting point as you can see in the following code). So, I got RB/INT external interrupt to work. No problem there. However I simply cannot get RB port change interrupt to work so any help is appreciated. I also have two questions related to RB port change and RB0/INT external interrupts. 1. What should be correct TRIS settings for those ...I'm trying to get Timer0 set up in order to create a 50Hz duty cycle to control standard hobby servos. The following code has the correct duty cycle and sweeps the pulse width from ~1msltPWlt~2ms. The problem is that PW is low instead of high, and the remainder of the 20ms is high instead of low. I have changed the output of the pins in the interrupt to reverse the problem but nothing seems to fix it. The sweep is just to test the code. I will have to control each servo individually later on. I am also unable to change ...I pieced together a code that outputs an R/C PWM signal. I had it working correctly until the chip died on the EasyPIC6 board. I replaced it with an identical chip and reprogrammed it. Now, instead of getting a 20ms duty cycle on the oscilloscope, I'm getting a 60us duty cycle. The chip is a 16f876 and I've posted the code below. It did not change between the last good programming of the old chip and the first programming of the new chip. The oscillator is the 8MHz crystal that came with the board. Timer0 is set to tick every 50us, ...I thought this was pretty straight forward, but my calculation doesn't match what I'm seeing in the MPLAB simulator. Microchip: PIC16F684 IDE: MPLAB 7.50 C Compiler: PICC 9.50 Settings: Clock source: internal oscillator Clock frequency: 4MHz TMR0 prescaler: 1:256 Code: #include <htc.h> #include <pic.h> unsigned long pulseCount; static long timerSpeed = 1000000; // Fosc / 4 = 4MHz / 4 static unsigned char timerPrescaler = 256; double overflowPeriod; int RPM; int toggle; int lastRA5Value; void main() { toggle = ...I've been searching and Googleing all afternoon and can't find the answer. I'm new to interrupts on the PIC. I'm using a 16F687. I want PORTB bits 5,6,7 to generate an IOC, but it doesn't work. I suspect it's an initialization problem, but I just can't figure it out. Here's my sloppy code. Code: Select all void Init() { OSCCON = 0b01110001; // setup for internal oscillator at 8Mhz. INTCON = 0b11011000; // Why do the following 3 lines cause compiler errors??? //INTCON.GIE = 1; ...Hello! I am new on the mikroC Pro (worked with CCS C) compiler and have a general question about a Timer to drive a Status-LED for displaying the work of the system. With TMR0 and it's interrupt i count up an integer and switch the LED on RC0 every 500ms on/off. Code: Select all unsigned int cnt_SystemLed = 0; void init() { ADCON0 = 0; ADCON1 = 0; TRISC0_bit = 0x00; RC0_bit = 0x00; // Timer0 Registers: // Prescaler=1:8; TMR0 Preset=6; Freq=1.000,00Hz; Period=1,00 ms OPTION_REG.T0CS = 0;// bit 5 TMR0 Clock Source Select bit:0=Internal Clock (CLKO) / ...Hi Tahmid, I am using mickoC and proteus for simulation. Please tell me some advices regarding the problem of this code. This code is for our traffic light project and I've got a problem with the push button(which is the interrupt), it won't work. Code: int j=5; int i=0; int count = 0; void interrupt(void) { for(i=0;i<5;i++) { PORTA = 0xFF; ...Hi guys, firstly, great forum. I can't find a more active microcontroller forum on Google! I've been toying around with the 16F877 and I've had some problems, pertaining to Timer0 interrupt and ADC. The problem: Enabling timer0 interrupt, affects my ADC values. Specifically when timer0 interrupt is disabled, my ADC values range properly from 0 -gt 1024. But with timer0 interrupt enabled, ADC values range from 512 -gt 1024. Weird problem! Do allow me to share the code: Code : void timer0_enable (void) { /* Initialise Timer 0 */ OPTION = 0b11010111; /* Set TMR0 to ...Code : #include <pic.h> void main(void) { int tmp; TRISB = 0b00000000; RBPU = 0; // Use internal pullups T0IE = 1; // Enable interrupt on TMR0 overflow INTEDG = 1; // falling edge trigger the interrupt INTE = 1; // enable the external interrupt GIE = 1; // Global interrupt enable tmp=1; for(;;) { CLRWDT(); // Idly kick the dog } } static void ...dear all, my stopwatch seems like not working. as for timing, it should be inside the interrupt loop together with my external interrupt (infrared sensor), * once the infrared sensor detect signal, the stopwatch will start to count, once detect again the stop watch will stop. based on my current code, it seems like when the infrared detect, the stopwatch will run , but when a second interrupt occur, the stopwatch cannot stop. May some one help to edit my code. i had already cleared all possible flag after each interrupt. #include ltpic.hgt #define _XTAL_FREQ 4000000L //__CONFIG (FOSC_INTRCIO amp WDTE_OFF amp PWRTE_ON ...I'm new to embedded program and mikroC.I am writing a program that uses a time before TMR0 overflow happens.how can I Decrement increment the TMR0 value before overflow?what is wrong in following program? void interrupt(void) { if(INTCON.INTF) { PORTA.f2 ^= 0x01; //toggle LED PORTA.f2 ^= 0x01; //toggle LED INTCON.INTF = 0; //TMR0 = 128; } else { PORTA.f2 = 0x00; INTCON.INTF = 0; } // INTCON amp= ~0x04; //clear Timer0 interrupt flag (T0IF = 0) } // A program that initalize TMR0 register void init_TMR0(void) { //initialize TMR0 INTCON amp= ~0x80; //disable all interrupts (GIE = 0) OPTION_REG.T0CS = 0x00; // OPTION_REG amp= ~0x20; //TMR0 uses the internal clock, Fosc/4 (T0CS = 0) OPTION_REG |= 1; //prescaler is assigned to the Watch Dog ...Code : #include <pic.h> void main(void) { int tmp; TRISB = 0b00000000; RBPU = 0; // Use internal pullups T0IE = 1; // Enable interrupt on TMR0 overflow INTEDG = 1; // falling edge trigger the interrupt INTE = 1; // enable the external interrupt GIE = 1; // Global interrupt enable tmp=1; for(;;) { CLRWDT(); // Idly kick the dog } } static void interrupt isr(void) ...I am now doing a project in which I using Timer 0 Interrupt and UART transmission at the same time. At first, it works perfectly before I add in UART transmission configuration. Once I add in UART transmission configuration, the program will never run the Interrupt function. Do any one can tell me what is the problem? //Configuration //================================================== ================ __CONFIG ( 0x3E31 ); #define BAUD 9600 #define _XTAL_FREQ 4000000 //#define DELAY 28 #define ALLOW_RECIEVE RA5 //#define TRAN_SONIC RB2 #define LED1 RB5 #define SONIC_IN RC2 #define RX_PIN RC7 #define TX_PIN RC6 void pic_init(void); int counter; static void interrupt isr(void) { if(TMR0IE ampamp TMR0IF) { //TMR0 Overflow ISR //Clear Flag TMR0IF=0; if (counter gt 500) { LED1 = 1; } else LED1 = 0; counter++; if (counter gt 1000) { counter = ...the following example is simply my previous interrupt example modified to use timer1 (16 bit timer). 16 bit timer gives greater number range, bit the prescalar is limited to divide by 8 max. 16F887 xtal = 20MHz int = 100ms portA shows when ISR is entered by inverting the LED status portB shows TMRL portC shows TMRH portD shows PIE1 ============================================== if you want a 20ms (50hz) interrupt then change the timer1 L amp H values in the main program and ISR to... TMR1L = 2C TMR1H = CF obviously you'll need a scope to see 50Hz! ============================================== Code: Select ...
