why my output is changinh the state

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
saravana_3
Posts: 61
Joined: Thu Dec 20, 2007 4:23 pm
Location: singapore
Contact:

why my output is changinh the state

Post by saravana_3 »

Hi friends,
I am using the easy pic5 and using the pic16f877a
I am doing the master/ slave communication. if I press the PORTB,7 PORTD,0 to PORTD,7 will lights up and latched, if again i press the RB7 all the PORTD out put will off. it is the example of one bit.
the problem is if i keep on pressing the PORTB,7 the output of the PORTD is keep on changing from ON to OFF, and OFF to ON
why it is happening in this way. on my previous programs if i continiously press the button it wont change the state.
my configuration bit is watch dog off, low power off, brown out enebled, oscillator is HS
I have attached the code, any one help me to fix my problem.

thanks
Saran

Code: Select all

* Project name:
     Rs485buttononlcd_master
 * Copyright:
     (c) MikroElektronika, 2005.
 * Revision History:
     20060722:
       - initial release
 * Description:
     This is a simple demonstration on how to use the mikroC's RS485 library.
     It is being used in pair with the Rs485buttononlcd_slave project. Master (this
     machine) initiates communication with slave by sending 3 bytes of data in form of
     two byte command and one byte of sampled PORTD input to the slave with designated
     slave address (160). The slave accepts rs485 packets and prints received PORTD
     value on lcd.
     Several situations are shown here:
       - RS485 Master Init sequence;
       - Data sending master-to-slave with designated slave address;
     Also shown here, but not strictly related to RS485 library, is:
       - Function calling from the interrupt routine - which data is to be saved,
         and how.
     For further explanations on RS485 library, please consult the mikroC Help.
 * Test configuration:
     MCU:             PIC16F877A
     Dev.Board:       EasyPIC3
     Oscillator:      External, 8.000 MHz
     Ext. Modules:    RS485 on PORTC
     SW:              mikroC v6.0
 * NOTES:
     - Be careful to initialize the USART module before performing RS485 init!
     - RS485 module is connected to PORTC, i.e. where the USART module is.
 */
  /*
  NOTE: RB0>RD0, RB1>RD1, RB2>RD2, RB3>RD3, RB4>RD4, RB5>RD5, RB6>RD6
        RB7>RD7 SELECT ALL SWITCH   (PORT B INPUT, PORT D OUTPUT)
        WHEN PRESS THE RB0 IF CLEAR SET THE RD0 IF SET CLEAR THE RD0 AND RD7
        RB0 TO RB6 SAME OPERATION.
        WHEN PRESS THE RB7 SET ALL THE RB0 TO RB7, WNEN PRESS AGAIN
        CLEAR ALL THE RB0 TO RB7
        RC0>RA0, RC1>RA1, RC4>RA2, RC5>RA3  (PORT C INPUT, PORT A OUTPUT)
        RA0 TO RA3 NON RELATED OUTPUTS
        OK PORTD OUTPUT AT THE MASTER AND SLAVE WHEN INPUT AT THE PORTB
        OK PORTA OUTPUT AT THE MASTER AND SLAVE WHEN INPUT AT THE PORTC
        */

        /*
        CONTINIOUS TRANSMISSION FROM THE MASTER TO SLAVE WHEN THE WIRE BREAK IN BETWEN
        THAT CAN BE MONITORED IN THE PORTE,0 ONCE THE COMMUNICATION BREAKDOWN
        BY REMOVING THE WIRE THEN THE PORTE,0 WILL OFF
        */


 unsigned int COUNT1, COUNT2, COUNT3, COUNT4, COUNT5, COUNT6, COUNTER_STATUS;

 char Output_Status , Output_Stat;

char dat[10];                                // buffer for receving/sending messages
char i,j;
// global flegs and counters
char send = 0;
unsigned int SamplePeriod, timer1_count = 0;

// timer1 settings
const char timer1_prescaler = 3;             // timer1 prescaler : real value | timer1_count value
                                             //                    -----------+-------------------
                                             //                         8     |        3
                                             //                         4     |        2
                                             //                         2     |        1
                                             //                         1     |        0

const unsigned int ticks_per_second = 1000;  // set timer1 overflow period : 10     -> 100msec
                                             //                              100    -> 10msec
                                             //                              1000   -> 1msec
                                             //                              10000  -> 100us
                                             //                              100000 -> 10us
                                             // note : for values under 10 and over 100000
                                             //        user must make sure that timer1 supports them
                                             //        with settings in config_timer1() function
                                             //        and change these settings if neccessery

// communication protocol constants (user defined)
const char COMMAND = 0x0AA;
const char DATA = 0xF0;
const char LCD_WRITE = 0x55;
const char COMMANDD = 0x0FF;
// auxilary variables
unsigned int temp;                           // sampled value temporary storage variable
unsigned long tmr1_temp;                     // timer1 reload value storage variable

//-------------- Interrupt routine
void interrupt(){
  if( PIR1.RCIF == 1 ) {                     // if uart interrupt
    RS485master_receive(dat);                // receive rs485 packets
//                                             // Note : this is uneccessery in this example
//                                             // considering that master does not expact
//                                             // any kind of response from slave
  }
  else
    if( PIR1.TMR1IF == 1 ) {                 // if timer1 interrupt
      timer1_count++;                        // increment counter
        if (timer1_count == SamplePeriod) {  // if sample period is reached
          timer1_count = 0;                  // clear counter
          send = 1;                          // set send flag
        }
      TMR1H = tmr1_temp >> 8;                // reload timer1 high
      TMR1L = tmr1_temp;                     // reload timer1 low
      PIR1.TMR1IF = 0;                       //clear timer1 interrupt flag
    }
}//~


void Configure_timer1(char SamplePeriodMs){
  SamplePeriod = SamplePeriodMs;             // set sample period

  T1CON = 0;
  T1CON.TMR1CS = 0;                          // internal clock (Fosc/4)
  T1CON |= timer1_prescaler << 4;            // set timer1 prescaler

  tmr1_temp = (Get_Fosc_kHz()*1000)/(4*(1 << timer1_prescaler)*ticks_per_second);
  tmr1_temp = 65535 - tmr1_temp;             // calc timer1 reload value (period = 1msec)

  TMR1H = tmr1_temp >> 8;                    // load timer1 high
  TMR1L = tmr1_temp;                         // load timer1 low

  T1CON.TMR1ON = 1;                          // enable timer1
}

void configure_interrupts(){
  PIE1.RCIE = 1;                             // enable uart receive interrupts
  PIE1.TMR1IE= 1;                            // enable timer1 interrupt
  INTCON.PEIE = 1;                           // enable periferal interrupts
  INTCON.GIE = 1;                           // global interrupt enable
  PIE2.TXIE = 0;
}

void main(){
long cnt = 0;





  ADCON1 |= 0x06;                            // set all pins as digita I/O
  CMCON  |= 0x07;                            // disable comparators
  PORTD = 0;
  TRISD = 0x00;                        // set PORTD as output
  PORTB           = 0X0;
  TRISB           = 0XFF;
  //PORTA           = 0x0;
//  TRISA           = 0X00;
  PORTE           = 0;
  TRISE           = 0;
  PORTC           = 0;
  TRISC           = 0x33;       //30
  USART_init(19200);                         // initialize usart module
  Delay_ms(1000);

  Rs485master_Init(&PORTC, 2);               // intialize mcu as master


  dat[0] = 0xFF;     //AC
  dat[1] = 0xF0;     //F0
  dat[2] = 0x0F;      //0F
  dat[4] = 0;                          // ensure that message received flag is 0
  dat[5] = 0;                          // ensure that error flag is 0
  dat[6] = 0;

  Rs485master_Send(dat,1,160);
  Configure_timer1(1);                       // configure timer1 & set sample period in msec
 Configure_interrupts();                    // enable interrupts

 STATUS      = 0X03;
PORTA       = 0X05;
PORTB       = 0X06;
OUTPUT_STATUS = 0X21;
COUNT1        = 0X22;
COUNT2        = 0X23;
//COUNTER_STATUS  = 0X24;
COUNT3          = 0X25;
COUNT4          = 0X26;
COUNT5          = 0X27;
//COUNT6          = 0X28;
//test_bit        = 0X29;
Output_Stat       = 0X30;
PORTA           = 0x0;
TRISA           = 0X00;
//PORTB           = 0X0;
//TRISB           = 0XFF;
//PORTC           = 0;
//TRISC           = 0x33;       //30
//PORTD           = 0;
//TRISD           = 0;
//PORTE           = 0;
//TRISE           = 0;
 Output_Status   = 0;
 Output_Stat       = 0;
// input = PORTA;
// oldinput = input;
  //while (1)           // endless loop
  do{



   asm {




;-----initialise PortB and Output_Status-------------
	movlw	0x00
	;movwf	portB
	movwf	portD
	movwf	Output_Status
  movwf Output_Stat
;-----------------------------------------------------
Start:


Test_Bit7:
         BTFSS   	portB,7
		goto	Test_Bit8		;--- A5 not pressed Jump to next bit
;---A4 pressed---
		BTFSS   	Output_Status,7
		goto	Set_BitB7		;----B5 already Clear so Set


;-----C1 already set so Clear it
		bcf	portD,0
		bcf	portD,1
		bcf	portD,2
		bcf	portD,3
		bcf	portD,4
		bcf	portD,5
		bcf	portD,6
		bcf	portD,7
		bcf	Output_Status,0
		bcf	Output_Status,1
		bcf	Output_Status,2
		bcf	Output_Status,3
		bcf	Output_Status,4
		bcf	Output_Status,5
		bcf	Output_Status,6
		bcf	Output_Status,7
		goto	wait_to_Resease_A7		;--- wait_to_Resease_A4

;-------------B5 already Clear so Set
Set_BitB7:
		bsf	portD,0
		bsf	portD,1
		bsf	portD,2
		bsf	portD,3
		bsf	portD,4
		bsf	portD,5
		bsf	portD,6
		bsf	portD,7
		bsf	Output_Status,0
		bsf	Output_Status,1
		bsf	Output_Status,2
		bsf	Output_Status,3
		bsf	Output_Status,4
		bsf	Output_Status,5
		bsf	Output_Status,6
		bsf	Output_Status,7

		goto	wait_to_Resease_A7		;--- wait_to_Resease_A4


;-------------------------------------------
wait_to_Resease_A7: }
Delay_ms(300);    //THIS TIMING IS TO ADJUST THE MASTER TO SLAVE TRANSMISION TIME
asm {


;------------end of bit-2 ----------------------------------------------
Test_Bit8:




   }

     //===========================================================================
   // cnt++;
//      if (dat [5] )   {
//      PORTE = 0xAA;   //AA
//      }
      if (dat[4])  {
      cnt = 0;
      dat[4] = 0;
      j  = dat[3] ;
      for (i = 1; i <= dat[3]; i++) {       //i++

      PORTE = dat[0];       //i-1
      }
      dat[0] = dat[0+1];   //[0+1]
      Delay_ms(10);
      RS485master_send (dat,1,160);
      }



 //==============================================================================

      if (send == 1) {                         // if send fleg is set
      dat[0] = COMMAND;                      // =
//dat[1] = LCD_WRITE;                   // |
      dat[2] = PORTD;                        // | fill rs485 packet with appropriate values
      dat[5] = 0;                            // |
      dat[6] = 0;                            // =
     Rs485master_Send(dat,3,160);           // send packet
      send = 0;
      }                        //clear send flag
     Delay_ms(30);            //THIS TIMING TO ADJUST THE BUTTON SCANNING



     asm {
     goto Start
     }

  } while(1);

}
saran

Post Reply