Author Topic: info/help C language programming pic  (Read 10380 times)

Offline cc600

  • Chief squatting Hard
  • *
  • Posts: 13
  • Post quality +1/-0
  • Acidmods User
info/help C language programming pic
« on: November 12, 2013, 07:14:51 AM »
hello

I want to write my own rapid fire to 360 C language
( not for profit , just a personal desire ) to try programming peak.

I look at the asm sources to get an idea or try to understand a bit how the programs but I is not knowledge in assembly language not so obvious .....

I started in a small test of pressing the Sync button and LED lights up , but nothing is happening ( I doubted a little), yet without compilation error .....
my code just for lighting an LED :

Code: [Select]
# pragma chip PIC12F683

/ / pin name
# define rttriger GPIO.0
# define lttriger GPIO.1
# define sync GPIO.3
# define LED GPIO.2

void Init ()
{
     // Set Frequency 8mhz
   OSCCON = 0b01110001 ;
 
     // Configure GPIO Port
   ANSEL = 0b00000011;     // Configure pins GP0 , GP1 as ANALOG
   Trisio = 0b00001011;    // GP0 , GP1 GP3 are input , rest is output
   GPIO = 0xFF;        // Set all pins to output high (0x00 = low)

     // Set Comparator
   CMCON0 = 7;        // Comparator is turned OFF
 }
void main ()
{
   INIT();

   while(1)
  {
    adc = ADC_Read(0);   // Get 10-bit results of AD conversion for GPIO0
    if(adc == 0)
    {
           //Trigger is not pulled
           ANS0_bit = 1;
           
      if ( adc > 409)
       {
//Trigger is pulled
             ANS0_bit = 0;
             RT = 0x00; // LOW RT ?
             sps;
             Delay_Cyc(adc);
        }
     }
  }; // End of run loop forever
} //End of main

I have wanted to know if possible to have a base in C language
I do not demand a complete code , just a base to see and understand how I look a bit on the net, the 12F683 datasheet , etc.
but a little hard to understand the how of why ...... and little explanatory thing about it .


my goal was to begin to make a rapid fire , one mode, the easiest possible way to get familiar with .

thank you
« Last Edit: November 27, 2013, 06:19:58 AM by cc600 »

Offline RDC

  • Administrator
  • Around the block
  • *
  • Posts: 2609
  • Post quality +90/-2
  • Gender: Male
  • The CGnome Project
Re: info/help C language programming pic
« Reply #1 on: November 12, 2013, 10:36:49 AM »
You are not going to get someone to walk you thru coding in C from the beginning, that's what a classroom and a teacher is for.

The first thing you need to do is Google it. If you can not find any information on programming ins C and the compiler you are using, then you are not looking for it.

Every C compiler is a little bit different also, so you will need to know what commands your compiler can use, or use one that someone else has already done something in so all of the commands are the same.

I use MikroC here, and while it looks similar to that, all C does, it is not the exact same and that would not compile here.

That code you have written there will not make an LED blink if you press Sync, that Sync Input is not even in the code loop there.

In MicroC, and using a 12F683, to make one of the controller LEDs blink with GP2 while you press Sync that is connected to GP3, you could do this..

Code: [Select]
/*******************************************************************************
PIC12F683 @ 8MHz
Code for Blinking an LED on GP2 if Sync Button on GP3 is pressed
AcidMods
*******************************************************************************/

#define LED GP2_bit
#define SY GP3_bit

void INIT(){
OSCCON = 0b01110111;                    // Set 8mhz Frequency
CMCON0 = 7;                             // Comparator off
ANSEL = 0;                              // Analog Off
TRISIO0_bit = 0;                        //
TRISIO1_bit = 0;                        //
TRISIO2_bit = 0;                        // LED (set to Input or it will interfere with the controller being able to use it)
TRISIO3_bit = 1;                        // SYNC Button
TRISIO4_bit = 1;                        //
TRISIO5_bit = 1;                        //
}


void main() {                           // Start of Main

 INIT();                                // Initialize the PIC

while (1){                              // Start of Run Forever Loop

 while (SY == 0){                       // While the SYNC button is pressed
  TRISIO2_bit = 0;                      // Set LED pin to Output (must set back to an Input when finished with the LED)
  LED = 1;                              // Turn LED on
  delay_ms(300);                        // Time LED is on, 300ms
  LED = 0;                              // Turn LED off
  delay_ms(50);                         // Time LED is off, 50ms
  }
  TRISIO2_bit = 1;                      // Turns LED off and keeps the pin TRIS so the controller can use the LED
 }                                      // End of Run Forever Loop
}                                       // End of Main


Again, Google whatever you are trying to figure out first, it is all out there, you just have to look for it. People are around here are glad to help, but they are not going to do all of the ground work for you.
« Last Edit: November 12, 2013, 10:41:27 AM by RDC »
Screwing up is one of the best learning tools, so long as the only thing you're not learning is how to screw up.

Offline cc600

  • Chief squatting Hard
  • *
  • Posts: 13
  • Post quality +1/-0
  • Acidmods User
Re: info/help C language programming pic
« Reply #2 on: November 12, 2013, 01:08:11 PM »
thank you for the answer man

I do not want to do all the work for me I'm still a "newbie" in C programming I still learn every day (and I think not having finished ..)

it's just to satisfy a personal curiosity

this is 2/3 days I work on has to look for information about why and how, but I began to mix my brushes with all that I have read the right and left

I'm just looking for a base not a complete code in order to study and understand the above

In any case, thank you for this piece of code will put it very useful already

hi

even a small problem peak does what I ask him, but there are still some gray area for me

for my test I put him three modes by his works against it crashes and stops after a tamping the trigger

the main.c:

Code: [Select]

/*******************************************************************************
   PIC12F683 @ 8MHz
        Code for simple rapid fire for xbox 360
                      for [url=https://www.acidmods.com]www.acidmods.com[/url]
*******************************************************************************/
void main()

{// Start of Main

    int mod = 0, i, sps[3] = {0 , 0, 0};
   
   INIT();                                // Initialize the PIC

while (1)
{                             
    if (SYNC == 0)
    {                    //if the SYNC button is pressed
         
                mod++;
            if(mod > 3)
             mode = 1;

           for(i = 0; i < mod; i++)
           {
           TRISIO2_bit = 0;// Set LED pin to Output
           LED = 1;// Turn LED on
           delay_ms(300);// Time LED is on, 300ms
           LED = 0;  // Turn LED off
           delay_ms(300); // Time LED is off, 50ms
           }

     }
     
    switch(mod)
     {
     case 1:

        TRISIO5_bit = 0;     //set RT pin to output

        if(rt == 1)
        {
          sps[0] = (1000/10)/2;
          sps[0];
        }

       break;
     
      case 2:

          TRISIO4_bit = 0;      //set LT pin to output
          TRISIO5_bit = 0;     // set RT pin to output

          if(rt == 1 || lt == 1)
          {
            sps[1] = (1000/18)/2;
            sps[1];
          }

         break;
     
      case 3:

      break;
      }


     TRISIO2_bit = 1;       
     TRISIO4_bit = 1;
     TRISIO5_bit = 1;
 }                                       
 
}                                       


a big thank you to the RDC code and explanation

I am trying to see to add programming mode, hold the left stick, etc ....
next update ..  :hifive:

.hex for those that want to test :
https://drive.google.com/file/d/0B5jjXb7Hc_E-S3B0b2otMWFWZGM/edit?usp=sharing

ps moderator: I do not know if my post and always in the right section

[admin]Your fine with right setion stop double and tripple posting last warning[/admin]
« Last Edit: November 23, 2013, 07:16:11 PM by Rodent »

Offline RDC

  • Administrator
  • Around the block
  • *
  • Posts: 2609
  • Post quality +90/-2
  • Gender: Male
  • The CGnome Project
Re: info/help C language programming pic
« Reply #3 on: November 16, 2013, 11:22:29 AM »
The Triggers are Analog, not Digital. So that bit of code you're using to tell if they are pressed

if (RT == 1)

will not work correctly at all. You have some reading and testing to do now as Analog is a bit different than a simple 0 or 1 Digital button.

Also, depending on what version of controller you're using there, they may go from Hi to Lo when pulled, or from Lo to Hi and everything in between, but in either case they're Analog, not just off or on like a button on the controller.

You need to setup those IO pins so they are default Inputs and Analog, then you change them 'on the fly' in code, as they need to be Analog Inputs to tell if the Trigger is pulled or not, then changed to Outputs to force the line back to a Hi or Lo state, depending on the controller version, so the controller 'thinks' the Trigger is not pulled, even though it is pulled, and then switched it back to an Input again so it can tell if it's still pulled or not.

I'm not going to code all of that out, but it needs to be done something like this..

In the INIT:

Set the IO for LT/RT as an Input and make it Analog

In Code:

Read the Analog value from that pin

If LT/RT is not pulled, do nothing and leave the pin as an Analog Input so it does not interfere with normal operation

If LT/RT is pulled, set the IO to an Output and make it Hi or Lo (depends on controller) for one cycle of your SPS timing, then set it back to an Input and Analog and check the value again. If it's still pulled do another Output cycle, if it's not pulled do nothing and leave it as an Analog Input.
Screwing up is one of the best learning tools, so long as the only thing you're not learning is how to screw up.

Offline cc600

  • Chief squatting Hard
  • *
  • Posts: 13
  • Post quality +1/-0
  • Acidmods User
Re: info/help C language programming pic
« Reply #4 on: November 18, 2013, 04:27:58 PM »
thank you for the advice!  :cool:

I see what you mean, but hard to understand how certain functions of the peak

this is what I did:  :dntknw:

Code: [Select]

/*******************************************************************************
PIC12F683 @ 8MHz
Code for simple rapid fire for xbox 360
AcidMods
*******************************************************************************/

#define LED          GP2_bit
#define SYNC         GP3_bit
#define rt           GP5_bit
#define lt           GP4_bit


void INIT()
{

   OSSCCON = 0b01110001;     //set int osc to 8MHz
   CMCON0 = 7;                             // Comparator off
   CMCON1 = 0;
   ANSEL = 0b00101000;                              // Analog On ?
   TRISIO0_bit = 0;                        //
   TRISIO1_bit = 0;                        //
   TRISIO2_bit = 0;                        // LED (set to Input or it will interfere with the controller being able to use it)
   TRISIO3_bit = 1;                      // SYNC Button
   TRISIO4_bit = 1;                      // Trigger LT
   TRISIO5_bit = 1;                        // Trigger RT
     
}


void main()
{// Start of Main

    int mode = 0, i, sps = (1000/10)/2;
    unsigned int adcValue;
   
   INIT();                                // Initialize the PIC
   
while (1)
{                              // Start of Run Forever Loop

    if (SYNC == 0)
    {//if the SYNC button is pressed
           
         mode = 1;
                   
           for(i = 0; i < mode; i++)
           {
             
               TRISIO2_bit = 0;// Set LED pin to Output (must set back to an Input when finished with the LED)
               LED = 1;// Turn LED on
               delay_ms(300);// Time LED is on, 300ms
               LED = 0;  // Turn LED off
               delay_ms(300); // Time LED is off, 300ms           
           }

     }
     

    if(mode)
      {
          adcValue = ADC_Read(5);   //read analog value pin 5 (RT) ??

          if(adcValue > 0)
          {

             Delay_Cyc(adcValue); //..... ???

                    TRISIO5_bit = 0;
                     sps;

          }
      }

     TRISIO2_bit = 1;// Turns LED off and keeps the pin TRIS so the controller can use the LED
     TRISIO5_bit = 1;
 }                                          // End of Run Forever Loop
 
}                                       // End of Main



the "adc-read" function, MickroC returns a numerical value right?

it is at this level that it becomes a blur .... and datasheet for beginners is a headache ..

Offline RDC

  • Administrator
  • Around the block
  • *
  • Posts: 2609
  • Post quality +90/-2
  • Gender: Male
  • The CGnome Project
Re: info/help C language programming pic
« Reply #5 on: November 18, 2013, 05:54:18 PM »
This is wrong..

ANSEL = 0b00101000;                              // Analog On ?

It has Analog turned on GP3 and GP5. GP3 has no AN, and you have the Triggers on GP4 and GP5. That part needs to be..

ANSEL = 0b00110000;                              // Analog On GP4 and GP5


You need to take a few steps back from trying to make it work in the controller, and make some simple projects work on a breadboard first.

The default ADC setup returns a 10bit value (which is 1024 different numbers, or 0-1023) based on whatever you have the Voltage Reference set as. That can be the same as the VDD of the chip, or an External Voltage on the vRef pin, GP1.

0 = 0v, 1023 = Max voltage

If the Max voltage is 3v, then 0 = 0v, 1023 = 3v

Each bit value = 1024 / Voltage = per bit voltage, or 1024 / 3v = ~0.003v

So 512 = 1.5v in that example

The Triggers on the 360 controllers are either 1.5v or 1.6v, again depending on the version of controller being used and is typically used for the External vRef. They don't cover the full range from 0v to 1.5v or 1.6v either, you'll have to test with a DMM to see, or setup the code to read and store the values itself. If you use VDD as the vRef, then that will change based on battery voltage and mess things up. That all has to be taken into account when reading the value and deciding when to do something in code and how to set everything up to begin with.

How you setup the ADC and do all of that math is up to you, but I'd experiment on the breadboard first there and get the hang of how it all works first.

Screwing up is one of the best learning tools, so long as the only thing you're not learning is how to screw up.

Offline cc600

  • Chief squatting Hard
  • *
  • Posts: 13
  • Post quality +1/-0
  • Acidmods User
Re: info/help C language programming pic
« Reply #6 on: November 19, 2013, 05:07:01 AM »
RDC thank you for all these info   :#1:

I will study it in close, I back to work .....


Code: [Select]

/*******************************************************************************
PIC12F683 @ 8MHz
simple rapid fire for xbox 360
AcidMods
*******************************************************************************/

#define LED          GP2_bit
#define SYNC         GP3_bit
#define rt           GP5_bit
#define lt           GP4_bit


void INIT()
{
   OSCCON = 0b01110001;                    // Set 8mhz Frequency
 
   // Configure GPIO Port
   ANSEL = 0b00110000;      // Configure pins GP5, GP4 as ANALOG
   TRISIO = 0b00111000;     // GP5, GP4 GP3 are input, rest is output
 
   // INIT ADC
   ADCON0 = 0b10110110;          // right justified???
   ADON_bit = 1;               //turn on adc convertion
   Delay_ms(1000);          //necessary??
   GO_DONE_bit = 1;            //start conversion
   
   // Configure Comparator
   CMCON0 = 7;     
   }

void main()
{// Start of Main
   
    int  i, sps = (1000/10)/2;

    unsigned int adc;
   
   INIT();                                // Initialize the PIC
   
while (1)
{                              // Start of Run Forever Loop

         
         // adc = adc_read(5);          //get adc value

         
           // (1024/1.2)*3;

        if(rt == 0)
        {
         
           while (GO_DONE) continue;  //???polling for conversion completion
           
           if (adc_read (5) >= 408) // input voltage >= 1.2V
                {
                        rt = 0;
                        Delay_Cyc(sps);
                 }
           else
                   rt = 1;       
           
        } 
         
 }                                          // End of Run Forever Loop
}                                       // End of Main


I admit to be a little lost ...
with this line "if (adc_read (5)> = 408)" I read the analog value of RT = GP5?
and if it is greater than or equal to 1.2V normally should perform, not the SPS cycle?

but nothing is happening ........

I is not board or other test, just a pic and a handle ....

or

volt = (adc*3)/1024;


  if (adc >= 408 )
[admin]Please click edit to modify you tripple post again i will give you a permant warning you been warned![/admin]
« Last Edit: November 23, 2013, 07:14:58 PM by Rodent »

Offline cc600

  • Chief squatting Hard
  • *
  • Posts: 13
  • Post quality +1/-0
  • Acidmods User
Re: info/help C language programming pic
« Reply #7 on: November 23, 2013, 03:10:40 PM »
Code: [Select]

/*******************************************************************************
PIC12F683 @ 8MHz
simple rapid fire for xbox 360
AcidMods
*******************************************************************************/

#define LED          GP2_bit
#define SYNC         GP3_bit
#define rt           GP5_bit
#define lt           GP4_bit


void INIT()
{
   OSCCON = 0b01110001;                    // Set 8mhz Frequency
 
   // Configure GPIO Port
   ANSEL = 0b00110000;      // Configure pins GP5, GP4 as ANALOG
   TRISIO = 0b00111000;     // GP5, GP4 GP3 are input, rest is output
 
   // INIT ADC
   ADCON0 = 0b10110110;          // right justified???
   ADON_bit = 1;               //turn on adc convertion
   Delay_ms(1000);          //necessary??
   GO_DONE_bit = 1;            //start conversion
   
   // Configure Comparator
   CMCON0 = 7;     
   }

void main()
{// Start of Main
   
    int  i, sps = (1000/10)/2;

    unsigned int adc;
   
   INIT();                                // Initialize the PIC
   
while (1)
{                              // Start of Run Forever Loop

         
         // adc = adc_read(5);          //get adc value

         
           // (1024/1.2)*3;

        if(rt == 0)
        {
         
           while (GO_DONE) continue;  //???polling for conversion completion
           
           if (adc_read (5) >= 408) // input voltage >= 1.2V
                {
                        rt = 0;
                        Delay_Cyc(sps);
                 }
           else
                   rt = 1;       
           
        } 
         
 }                                          // End of Run Forever Loop
}                                       // End of Main


I admit to be a little lost ...
with this line "if (adc_read (5)> = 408)" I read the analog value of RT = GP5?
and if it is greater than or equal to 1.2V normally should perform, not the SPS cycle?

but nothing is happening ........

I is not board or other test, just a pic and a handle ....

or

volt = (adc*3)/1024;

   if (adc >= 408 )
« Last Edit: November 23, 2013, 03:24:31 PM by cc600 »

Offline RDC

  • Administrator
  • Around the block
  • *
  • Posts: 2609
  • Post quality +90/-2
  • Gender: Male
  • The CGnome Project
Re: info/help C language programming pic
« Reply #8 on: November 23, 2013, 04:27:50 PM »
First, you need to get off the controller and breadboard up some test setup until you can get the ADC working and understand how all of that works. Just make an LED turn on if a Potentiometer connected to an Analog Input is turned all the way up and off it's all the way down.

If you want a 'simple' crapid fire on the Triggers then use some Tact switch as the Input so when you press it the PIC can then pulse the Trigger line. The way you're going about it is the harder way, using the Trigger as the input and then having to switch to an Output and control, it, then back and forth and back and forth.

Either do something a little easier to start with, or take a few steps back and get it all figured out before trying to make it work on the controller.


Second, I don't know what you're coding in there, but in mikroC all you need to do is..
Code: [Select]
int whatever;

whatever = ADC_Read(5); // Then whatever is the Analog value on that pin. It's already setup and does it all for you to return the 10bit value.

if (whatever >= value_you _want _it_to_do_something)
 {
 then do something
 }


Third, no idea what vRef you're using or what controller version. There are too many missing variables for me to even guess at how to go about coding that there.

Fourth, see First.
Screwing up is one of the best learning tools, so long as the only thing you're not learning is how to screw up.

Offline cc600

  • Chief squatting Hard
  • *
  • Posts: 13
  • Post quality +1/-0
  • Acidmods User
Re: info/help C language programming pic
« Reply #9 on: November 24, 2013, 01:17:27 AM »
I tried several things but none works ........

for after what you said I was just using the function adc_read then according to the value it returns " if I understand " declare pin 5 with Trisio output and the number of sps

I test it on a CG multimeter to the value obtained by pressing RT and ~ 1.27 V

ex:

void Init ()
{
   OSCCON = 0b01110001 / / Set Frequency 8mhz

   / / Configure GPIO Port
   ANSEL = 0b00110000 / / Configure pins GP5 , GP4 as ANALOG
   Trisio = 0b00111000 / / GP5 , GP4 GP3 are input , rest is output
   / / GPIO = 0xff / / Set all pins to output high (0x00 = low)

   / / INIT ADC
   ADCON0 = 0x00 / / right Justified ? ?
   ADON_bit = 1 / / turn on adc convertion
   GO_DONE_bit = 1 / / start conversion

   / / Set Comparator
   CMCON0 = 7 / / Comparator is turned OFF
 }

void main ()
{
    int sps = (1000/10)/2;
    unsigned int adc;

  while (1)
 {
     while ( GO_DONE_bit ) continue / / wait for conversion completion
       ADON_bit = 0 ;

    adc = adc_read (5);
           
            if ( adc > 409)
             {
                   TRISIO5_bit = 0 / / or GPIO = 0xFF ;
                   sps ;
             }
            else
                   TRISIO5_bit = 1 ;
  }
}
init and good ? I have not done wrong or forget something ?

Offline SethMods

  • Registered BST
  • King of the Wii-tards
  • *
  • Posts: 189
  • Post quality +17/-1
  • Gender: Male
  • Acidmods User
Re: info/help C language programming pic
« Reply #10 on: November 24, 2013, 07:29:26 AM »
Unfortunately GPIO5 isn't an analog input.





Your options are
GPIO0 => AN0
GPIO1 => AN1
GPIO2 => AN2
GPIO4 => AN3 (Not a typo. AN3 maps to GPIO4.)

If you are using MikroC...which it looks like you are, then get rid of...

   / / INIT ADC
   ADCON0 = 0x00 / / right Justified ? ?
   ADON_bit = 1 / / turn on adc convertion
   GO_DONE_bit = 1 / / start conversion

MikroC will do this for you.


Also, get rid of...
     while ( GO_DONE_bit ) continue / / wait for conversion completion
       ADON_bit = 0 ;

It's all done for you.



What compiler are you using? MikroC, XC8,...?
« Last Edit: November 24, 2013, 09:54:30 AM by SethMods »

Offline cc600

  • Chief squatting Hard
  • *
  • Posts: 13
  • Post quality +1/-0
  • Acidmods User
Re: info/help C language programming pic
« Reply #11 on: November 24, 2013, 12:57:30 PM »
thank you

ok I thought ADCS1 could correspond to the analog pin 5

just a start ADC_init function, and it will do the necessary in my place?
even using adc_get_sample? I will not have value?

I use MickroC

Offline SethMods

  • Registered BST
  • King of the Wii-tards
  • *
  • Posts: 189
  • Post quality +17/-1
  • Gender: Male
  • Acidmods User
Re: info/help C language programming pic
« Reply #12 on: November 24, 2013, 02:05:15 PM »
I don't use MikroC, but from their documentation it looks like you only need ADC_Init if you are going to use ADC_Get_Sample.

If you use ADC_Read the ADC initialization seems to be built-in.

...but I could be wrong on that.

The example that they give is...
Code: [Select]
unsigned int temp_res;

void main() {
  ANSEL  = 0x04;              // Configure AN2 pin as analog
  ANSELH = 0;                 // Configure other AN pins as digital I/O
  C1ON_bit = 0;               // Disable comparators
  C2ON_bit = 0;
 
  TRISA  = 0xFF;              // PORTA is input
  TRISC  = 0;                 // PORTC is output
  TRISB  = 0;                 // PORTB is output

  do {
    temp_res = ADC_Read(2);   // Get 10-bit results of AD conversion
    PORTB = temp_res;         // Send lower 8 bits to PORTB
    PORTC = temp_res >> 8;    // Send 2 most significant bits to RC1, RC0
  } while(1);
}




But for your case I think it could be shortened to...





Code: [Select]
void Init ()
{
   OSCCON = 0b01110001 / / Set Frequency 8mhz

   / / Configure GPIO Port
   ANSEL = 0b00000011 / / Configure pins GP0 , GP1 as ANALOG
   Trisio = 0b00001011 / / GP0 , GP1 GP3 are input , rest is output
   / / GPIO = 0xff / / Set all pins to output high (0x00 = low)
   
   / / Set Comparator
   CMCON0 = 7 / / Comparator is turned OFF
 }
 
void main() {
    int sps = (1000/10)/2;
    unsigned int adc;


  do {
    adc = ADC_Read(0);   // Get 10-bit results of AD conversion for GPIO0
            if ( adc > 409)
             {
//Trigger is pulled Wait X
//Force release and Wait X                   
             }
  } while(1);
}
« Last Edit: November 24, 2013, 02:05:45 PM by SethMods »

Offline cc600

  • Chief squatting Hard
  • *
  • Posts: 13
  • Post quality +1/-0
  • Acidmods User
Re: info/help C language programming pic
« Reply #13 on: November 27, 2013, 06:20:25 AM »
edit my first post

Offline SethMods

  • Registered BST
  • King of the Wii-tards
  • *
  • Posts: 189
  • Post quality +17/-1
  • Gender: Male
  • Acidmods User
Re: info/help C language programming pic
« Reply #14 on: November 27, 2013, 07:43:16 AM »
Ok, a few things that I noticed...and RDC may have noticed more.


First, C is case-sensitive. So you have to be careful. You defined the init function as "Init", but then you try to call it as "INIT".

Second, your "if" statements aren't setup properly. Right now you have "if trigger is pulled" nested inside of "if trigger is NOT pulled". So you will never get to that second "if".  (Or at least it is "virtually" impossible. There is a very tiny chance.)
What you want to do instead is setup an if/else. "If the RT is pulled", "else the RT is NOT pulled".

if( ADC_read(0) > 409 ){  //RT Pulled
  //do something
}
else{  //RT Released
  //do something
}

Third, there is no need to mess with the ANS register once you are setup. ANS0 determines if GPIO0 is configured to be a digital input or an analog input. It won't affect the output. So it's fine for us to leave this as an analog input and toggle back and forth between analog input and digital output by just changing the TRIS value from 1 to 0 (respectively).  Does that make sense? I could understand if this were confusing.

Lastly you are still going to have to put the actual rapid fire functionality in there.

I hope this helps. You are doing well. C++ can be tough as can PICs. Learning them both at the same time can be especially challenging.



Also, one last note. You are getting warned about double-posting because of the way that you are responding. What you are trying to avoid is that your name doesn't come up twice in a row in the conversation.

So,...
cc600
RDC
cc600
RDC
cc600
SethMods
cc600

...is ok.


But,...
cc600
RDC
cc600
cc600

...is not ok.

« Last Edit: November 27, 2013, 07:47:45 AM by SethMods »

Offline cc600

  • Chief squatting Hard
  • *
  • Posts: 13
  • Post quality +1/-0
  • Acidmods User
Re: info/help C language programming pic
« Reply #15 on: November 27, 2013, 08:21:30 AM »
ok, I'm not English and a little difficult to interpret certain word so "google trad" but not terrible for conversations

sorry...

my main.c:
Code: [Select]
/*******************************************************************************
PIC12F683 @ 8MHz
Code for simple rapid fire for xbox 360
AcidMods
*******************************************************************************/

#define RT           GP0_bit
#define LT           GP1_bit
#define LED          GP2_bit
#define SYNC         GP3_bit

void Init ()
{
     // Set Frequency 8mhz
   OSCCON = 0b01110001 ;
 
     // Configure GPIO Port
   ANSEL = 0b00000011;     // Configure pins GP0 , GP1 as ANALOG
   Trisio = 0b00001011;    // GP0 , GP1 GP3 are input , rest is output
   GPIO = 0xFF;        // Set all pins to output high (0x00 = low)

     // Set Comparator
   CMCON0 = 7;        // Comparator is turned OFF
 }

void main()
{
    int sps = (1000/10)/2;
    unsigned int adc;

     Init();
 while(1)
  {
    adc = ADC_Read(0);   // Get 10-bit results of AD conversion for GPIO0
    if ( adc > 409)
       {
//Trigger is pulled
             TRISIO0_bit = 0;
             RT = 0x00; // LOW RT ?
             sps;
             Delay_Cyc(adc);
        }
    else
    {
        //Trigger is not pulled
        TRISIO0_bit = 1;
    }
  }; // End of run loop forever
} //End of main
« Last Edit: November 27, 2013, 08:25:14 AM by cc600 »

Offline SethMods

  • Registered BST
  • King of the Wii-tards
  • *
  • Posts: 189
  • Post quality +17/-1
  • Gender: Male
  • Acidmods User
Re: info/help C language programming pic
« Reply #16 on: November 27, 2013, 09:28:28 AM »
You are very close. The only problem now is with the actual rapid fire. I think all you need is something like this.

Code: [Select]
if ( adc > 409)
{
//Trigger is pulled
Delay_ms(sps); //Continue to allow trigger to be pulled

//Now we have to force trigger release
TRISIO0_bit = 0;
Delay_ms(sps);


//We are done firing one shot. We need to set TRISIO0 back to input
TRISIO0_bit = 1;
}



There are better ways to do this, but this is the most straightforward (I think).

Offline cc600

  • Chief squatting Hard
  • *
  • Posts: 13
  • Post quality +1/-0
  • Acidmods User
Re: info/help C language programming pic
« Reply #17 on: November 27, 2013, 02:49:47 PM »
thank you for all these explanations and assistance

new code:
Code: [Select]

/*******************************************************************************
PIC12F683 @ 8MHz
Code for simple rapid fire for xbox 360
AcidMods

      VDD-1|   |8-VSS
GP5/T1CKI-2|   |7-GP0/ANS0
  GP4/AN3-3|   |6-GP1/ANS1
 GP3/MCLR-4|   |5-GP2/ANS2
*******************************************************************************/

#define RT           GP1_bit
#define LT           GP0_bit
#define LED          GP2_bit
#define SYNC         GP3_bit

void Init ()
{
     // Set Frequency 8mhz
   OSCCON = 0b01110001 ;
 
     // Configure GPIO Port
   ANSEL = 0b00000011;     // Configure pins GP0 , GP1 as ANALOG
   Trisio = 0b00001011;    // GP0 , GP1 GP3 are input , rest is output
   GPIO = 0xFF;        // Set all pins to output high (0x00 = low)

     // Set Comparator
   CMCON0 = 7;        // Comparator is turned OFF
   
   PWM1_Init(19530); //Frequencies datasheet fosc8Mhz = 19.61kHz
 }

void main()
{
    int i = 0, sps = (1000/10)/2;
    unsigned int adc;

     Init();
 while(1)
  {
   if (SYNC == 0)
   {           // if the SYNC button is pressed
         TRISIO2_bit = 0;                      // Set LED pin to Output (must set back to an Input when finished with the LED)
         LED = 1;                              // Turn LED on
         delay_ms(300);                        // Time LED is on, 300ms
         LED = 0;                              // Turn LED off
         delay_ms(300);                         // Time LED is off, 300ms
  }

    adc = ADC_Read(1);   // Get 10-bit results of AD conversion for GPIO0
    if ( adc > 200 )
    {
       //Trigger is pulled
      //delay_ms(sps);   //Continue to allow trigger to be pulled
     
       //Now we have to force trigger release
       TRISIO1_bit = 0;
       PWM1_Set_Duty(63);
      sps;

       //We are done firing one shot. We need to set TRISIO0 back to input
       TRISIO1_bit = 1;

     }
   else
      TRISIO1_bit = 1; 
   
     Delay_ms(25);   

TRISIO2_bit = 1;
  }; // End of run loop forever
} //End of main


it works much better than the first test but I find that there are still some latency ....

Did I use PWM? for example you will be there detectable, COD WAW​​?

I do not quite understand the use of HI LOW

  adc> 409 does not work (nothing is happening)
« Last Edit: November 27, 2013, 04:06:49 PM by cc600 »

Offline SethMods

  • Registered BST
  • King of the Wii-tards
  • *
  • Posts: 189
  • Post quality +17/-1
  • Gender: Male
  • Acidmods User
Re: info/help C language programming pic
« Reply #18 on: November 27, 2013, 05:18:45 PM »
You make me want to cry.

Offline cc600

  • Chief squatting Hard
  • *
  • Posts: 13
  • Post quality +1/-0
  • Acidmods User
Re: info/help C language programming pic
« Reply #19 on: November 28, 2013, 12:42:59 AM »
thank you for all these explanations and assistance

new code:
Code: [Select]

void main()
{
    int i = 0, sps = (1000/10)/2;
    unsigned int adc;

     Init();
 
while(1)
  {
       adc = ADC_Read(1);   // Get 10-bit results of AD conversion for GPIO0
   
     if ( adc > 200 )
    {
       //Trigger is pulled
      sps;   //Continue to allow trigger to be pulled
     
       //Now we have to force trigger release
       TRISIO1_bit = 0;
       delay_ms(25);
       sps;

       //We are done firing one shot. We need to set TRISIO0 back to input
       TRISIO1_bit = 1;

     }
     else
      TRISIO1_bit = 1; 
   
     Delay_ms(25);   


  }; // End of run loop forever
} //End of main

works pretty good

Quote
Did I use PWM? for example you will be there detectable, COD WAW​​?

I think not ..... a lot of reading and testing is required ....

 
You make me want to cry.

sorry I read a little faster ... and I did not want

By cons I do not understand why when I put RT on GP0 and it automatically output
pressing RT has no effect while the same code works on GP1
« Last Edit: November 28, 2013, 12:53:43 AM by cc600 »

Offline SethMods

  • Registered BST
  • King of the Wii-tards
  • *
  • Posts: 189
  • Post quality +17/-1
  • Gender: Male
  • Acidmods User
Re: info/help C language programming pic
« Reply #20 on: November 28, 2013, 07:12:14 AM »
Ok, for now forget PWM. Instead of PWM all you need is to pull the trigger longer than you release the trigger by a certain percentage (3:1).

So for CoD WAW 10SPS just do...

Pull RT
Delay 75ms
Release LT
Delay 25ms

The different timing(75ms and 25ms) accomplishes exactly what PWM does but is much easier to implement.

Now let's look at (ADC >200).

ADC is taking the voltage that it is reading and converting it to a number between 0 and 1023. If the voltage is 0 then the result is 0. If the voltage is Vdd(The positive connection to the 12f683) then the result will be 1023. And it is linear so that if the voltage is Vdd/2 then the result will be 512.

So with a CG controller the triggers released rest around .20V and pulled go up to around 1.3V (or close to those numbers). If Vdd is 3.3V then you should receive .20V/3.3V * 1024 = 62 when the trigger is released. And 1.3V/3.3V * 1024 =  403 when the trigger is pulled.

To avoid the delay or stutter when shooting the first shot you have to have the mod respond before the XBox recognizes it as a trigger pull.

So for instance if the trigger released is .20V and the XBox recognizes a trigger pull at .50V(I'm guessing. I haven't checked.) Then you have to have the mod kick in between .20V and .50V.

Also, keep in mind that these numbers change as Vdd changes. So as your battery starts to die this may stop working. So it is important to run these numbers yourself and come up with the ADC value that works on your controller. Additionally you will get slightly different readings for each trigger. So you may be using a value that DOES work on LT but not RT. You have to test to find a good all around value that works for "everything".


Next, the command "sps;" does nothing. In C/C++ the value is evaluated, but nothing changes. It's pointless. What you need to be doing is putting sps into Delay_ms.

So you are saying sps = 1000/10/2; You are saying that there are 1000 ms in one second. If we want 10SPS then we have to divide that by 10. And because half of that time is going to be spent with pulling the trigger and half with releasing the trigger we divide that by 2.

So in this case for 10SPS we need to pull the trigger for 50ms and release the trigger for 50ms. That will give us 10SPS. (This ignores the discussion about PWM from above.)

Also, it's important to note that if you try to pull the trigger faster than the game will allow then the gun will stutter. Everyone has a different opinion on this, but my opinion is that 12SPS is the fastest you can really shoot on CoD...depending on the gun. So with your delay_ms(25) you are attempting 20SPS. Try slowing it down to see if that helps with the latency.


Your RF is still wrong.

It should look like this.

Code: [Select]
void main()
{
    int i = 0, sps = (1000/10)/2;
    unsigned int adc;

     Init();
 
while(1)
  {
       adc = ADC_Read(1);   // Get 10-bit results of AD conversion for GPIO0
   
     if ( adc > 200 )
    {
        //Trigger is pulled
        Delay_ms(sps); //Continue to allow trigger to be pulled

        //Now we have to force trigger release
        TRISIO1_bit = 0;
        Delay_ms(sps);


        //We are done firing one shot. We need to set TRISIO1 back to input
        TRISIO1_bit = 1;

     }


  }; // End of run loop forever
} //End of main

And really because we added TRISIO0_bit = 1; to the last line we don't need the 'else' part.

 

SMF spam blocked by CleanTalk
SimplePortal 2.3.5 © 2008-2012, SimplePortal