[ create a new paste ] login | about

Link: http://codepad.org/zUSApB0b    [ raw code | fork ]

C, pasted on Dec 31:
//MUCANWORK MOTOR DRIVER CODES

#include <main.h>
#USE SPI (SLAVE, BITS=8, STREAM=SPI_1)
#USE SPI (MASTER, MODE=0, BITS=8, STREAM=SPI_2)
#include <LCD.C>
 int sn,dk,st,t;
#INT_TIMER0
void  TIMER0_isr(void) 
{


}
#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
void PWM_init() 
{ 
   output_low(PIN_C1);   // Set CCP1 output low 
   output_low(PIN_C2);   // Set CCP2 output low 

   setup_ccp1(CCP_PWM);  // Configure CCP1 as a PWM 
   setup_ccp2(CCP_PWM);  // Configure CCP2 as a PWM 

   setup_timer_2(255,255,1); 
} 


void main()
{
PWM_init();
    setup_spi(spi_slave | spi_L_to_h | spi_clk_div_16);
   setup_adc_ports(AN0);
   setup_timer_2(T2_DIV_BY_1,249,1);      //50,0 us overflow, 50,0 us interrupt

   setup_ccp1(CCP_PWM|CCP_SHUTDOWN_AC_L|CCP_SHUTDOWN_BD_L);
   set_pwm1_duty((int16)0);
   SET_TRIS_D(0xFF);
  int k,g=0;
   int z=0;
   int yn=12,hz=13,dv=14;
   int gelen;
   while(TRUE)
   {

             if(spi_read()==23)
         {
      set_pwm1_duty(255);
         }


   if(g==0)
   {
    setup_spi(spi_slave | spi_L_to_h | spi_clk_div_16);
   }
   if(g==1)
   {          
             setup_spi(spi_master | spi_l_to_h | spi_clk_div_16);
            spi_write(11);
            spi_write(hz);
            spi_write(21);
            spi_write(yn);
            spi_write(31);
            spi_write(dv);
      
            g--;
   }
   z=0;
   printf(lcd_putc,"hiz:%d yon:%d dv=%d sy=%d",hz,yn,dv,k);
    while(input(PIN_D3))  
   {
    if(z==0)
    {
    k++;
    z++;
    }
        
   }

         if (SPI_DATA_IS_IN())  // SOLO VALE PARA MODO SLAVE
      {
               if(spi_read()==111)
         {
         g++;
         }
      
      if(gelen==11)
      {
      hz=spi_read();
      }
      if(gelen==21)
      {
      yn=spi_read();
      }
      if(gelen==31)
      {
      dv=spi_read();
      }
         if(spi_read()==11)
         {
         gelen=11;
         }
         if(spi_read()==21)
         {
         gelen=21;
         }
         if(spi_read()==31)
         {
         gelen=31;
         }
         
         if(spi_read()==99)
         {
         if(yn==1)
         {
          set_pwm1_duty(hz);
         }
         if(yn==-1)
         {
          set_pwm2_duty(hz);
         }
         }
         

         
    }
    
    

      }
   
   }
   /*if(gelen%100==1)
   {speed1=(gelen-1)/100;
   speed2=speed1;}
   if(gelen%100==2)
   {
   yn=(gelen-2)/100;
   }
   }
     if(yn==1)
     { set_pwm1_duty(speed1);
     }
     
     if(yn==-1)
     {set_pwm2_duty(speed2);
     } */


Create a new paste based on this one


Comments: