Sunday, August 2, 2015

PIC32 Tic-Tac-Toe: Demonstration of using touch-screen, TFT and the Protothreads threading library





I had previously used the Adafruit TFT display using my library (ported from the Adafruit Arduino library). I decided to optimize the library to improve drawing speed. The same display I use comes with a 4-wire resistive touch-screen as well. I decided to write a simple library for the touch-screen and give Protothreads a try. To incorporate all this, I thought it would be cool if I used these to make a simple game. Tic-tac-toe came to mind as a fun little demo.


I'm sure everyone's familiar with the game so I won't explain the rules there. The touch-screen is simply two resistive sheets placed on top of each other on top of the TFT screen. When it is pressed down at a given place, the two sheets make contact and a voltage divider is formed. Using the IO's and the ADC, this voltage is read in the X and Y directions to register a touch.

Here is a very good pictorial depiction of the resistive touch screen (taken from the Atmel AVR341 document):

So in order to read the touch, the X+ and X- points are applied power, and one of Y+ or Y- is read to read the x-coordinate. Then Y+ and Y- are applied power and one of X+ or X- is read to read the y-coordinate. X+, X-, Y+ and Y- are connected to four GPIO pins on the PIC32 that are configured to outputs when driving the touch-screen and analog inputs when reading. Every time the IO pin switches state, a long delay is provided to allow the outputs to stabilize. Alternately, the ADC is significantly slowed down to negate effects of capacitive charging by high source impedance. The library is written in the form of a simple state machine cycling through its states every few milliseconds, decided by the application calling the library functions. In my application, I use 5 milliseconds.

To organize the game, I've made use of the Protothreads threading library. Protothreads is a very light-weight, stackless, threading library written entirely as C macros by Adam Dunkels. Bruce Land has ported Protothreads over for the PIC32. You can find more details on his excellent site: http://people.ece.cornell.edu/land/courses/ece4760//PIC32/index_Protothreads.html

There are two main executing threads, one is the main game thread and the other is a clock thread that keeps track of, and displays, time since the program was started. There is a third thread used to retrieve touch information. It is spawned by the main game thread when touch input is required. The main Protothreads functions (macros) I've made use of are:

PT_setup()
PT_INIT()
PT_SCHEDULE()
PT_SPAWN()
PT_WAIT_UNTIL()
PT_YIELD_TIME_msec()
PT_GET_TIME()

Pin connections:

BL (backlight): I left it unconnected, but you can connect it to 3.3V for backlight.
SCK: connected to RB14 on the PIC
MISO: left unconnected, since I'm not reading anything from the screen
MOSI: connected to RB11 on the PIC
CS: connected to RB1 on the PIC
SDCS: left unconnected as I'm not using the microSD card for this
RST: connected to RB2 on the PIC
D/C: connected to RB0 on the PIC
X+: connected to RA4 on the PIC
X-: connected to RB13 on the PIC
Y+: connected to RB5 on the PIC
Y-: connected to RB15 on the PIC
VIN: connected to 3.3V supply
GND: connected to gnd

Here is a demo of the game:



Besides the game itself, you can see the running clock on the bottom left right above the players' scores. To the bottom right you can see a flickering circle that is either green or red, depending on if it's player 1 or 2's turn, respectively. Once the game is over, you have the option of playing another game while score is being tracked.

Here is a link to the MPLABX project with all required header and source files:
https://drive.google.com/file/d/0B4SoPFPRNziHbURwVWVSN1c3VVE/view?usp=sharing

I have commented the code to make it fairly self-explanatory. If you have doubts or questions about anything, let me know and I'll add more detail. Let me know what you think!

44 comments:

  1. nice work syed g.

    ReplyDelete
  2. How do I calculate how many mosfet needed for a 1500w fullbridge inverter with very good efficiency.
    I use 24vdc input.
    I use irfp260n.
    Thanks

    ReplyDelete
  3. I use either hip4081A or ir2110

    ReplyDelete
  4. Hi Tahmid , Glad to see your work here. I have a question that is related to IR 2110. I want to understand the groud connection of high side driver with ac grid as a load in case of flyback inverter configuration.
    Thanks

    ReplyDelete
  5. Good work Tahmid, What optimizations have you made to the graphics library?

    ReplyDelete
  6. This blog is about using of touch-screen.Cartincoupon.com

    ReplyDelete
  7. What a nice Information are there in it.MyFirstSaving

    ReplyDelete
  8. I am quite interesting in this topic hope you will elaborate more on it in future posts. Keep it up the good work!

    ReplyDelete
  9. Interesting blog and great knowledge to share me thanks for sharing nice post.

    programmable logic controller

    tension controller

    ReplyDelete
  10. dear sir,
    my name is Than Toe Aung from mandaly.I am beginner of Arduino and PIC.I want to start my first project "arduino powered ac voltage stablizer" for my home.I have some difficulties on coding for unstable voltage like as wave,eg 121.5,119.8,120.3 and so on in 1minute. So I tried to search on google, than I get one.That is your voltage stabilizer project. I fairly read your code.But something else your project working on PIC, I need to use arduino. Please explain me for this code. "void StabilizeVoltage(void){
    unsigned int ADS;
    unsigned char V_LOW_CUT;
    if (flgAC){
    ADS = ADC_Get_Sample(Vin_CHANNEL);
    Voltage_FLOAT = (float)((float)ADS * 5.0 * 101.0 * Kc) / (1.3836 * 1023.0); "
    How i can use in arduino.
    Thank you sir,
    I am waiting for your reply.

    ReplyDelete
  11. It's great that you are telling us about these things.

    ReplyDelete
  12. LiveWireLabs.
    Join hands with LiveWireLabs, a resource augmentation entity, and have the top 2 percent of talent work for you for business analysis, software engineering, and digital media design. Visit our website for more information.
    staff augmentation

    ReplyDelete
  13. I hope to get a very useful Cleaning Services site for everything you need in this very special world

    ReplyDelete
  14. Great Job done your content is very help full.document registration service
    good blog. blog like these are are very helpful for us because we are into ac repairing and service. It is always good to read and upgrade ourselves

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete
  16. It's a Nice post! Thank you for sharing your knowledge to others, it was very informative and in depth one.
    R Training in Electronic City

    ReplyDelete
  17. Hello Everybody,
    My name is Mrs Cynthia Corvin. I live in USA and i am a happy woman today? and i told my self that any lender that rescue my family from our poor situation, i will refer any person that is looking for loan to him, he gave me happiness to me and my family, i was in need of a loan of S$250,000.00 to start my life all over as i am a single mother with 2 kids I met this honest and GOD fearing man loan lender that help me with a loan of usd$250,000.00 . Dollar, he is a GOD fearing man, if you are in need of loan and you will pay back the loan please contact him tell him that is Mrs Cynthia that refer you to him. contact Mr. Ferooz Mohammed ,via email:(feroozsuptoo@outlook.com) Thank you.

    ReplyDelete
  18. We offer the best quality Telemetry level sensors for monitoring tank level and reduces the cost of distribution and develop better client service. By using telemetry tank level sensor in a tank you can reduce the use of man power and amount of waste.
    Tank monitoring system using telemetry level sensor

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. This comment has been removed by the author.

    ReplyDelete
  21. I will recommend anyone looking for Business loan to Le_Meridian they helped me with Four Million USD loan to startup my Quilting business and it's was fast When obtaining a loan from them it was surprising at how easy they were to work with. They can finance up to the amount of $500,000,000.000 (Five Hundred Million Dollars) in any region of the world as long as there 1.9% ROI can be guaranteed on the projects.The process was fast and secure. It was definitely a positive experience.Avoid scammers on here and contact Le_Meridian Funding Service On. lfdsloans@lemeridianfds.com / lfdsloans@outlook.com. WhatsApp...+ 19893943740. if you looking for business loan.

    ReplyDelete
  22. Situs Agen Judi SBOBET, Agen SBOBET Terbesar, Agen SBOBET Dengan BOnus New Member, Agen SBOBET Bola Online, Judi Bola Deposit Termurah, Bandar SBOBET CS 24 Jam, Judi Online SBOBET, Daftar SBOBET Dengan Mudah, Cara Daftar Judi SBOBET, Bonus Besar Judi SBOBET, Judi SBOBET Deposit Pulsa, Judi SBOBET Terbaik, SBOBET Sepak Bola, Main Judi Bola Online SBOBET Sekarang, Judi Tembak Ikan Joker123, Tembak Ikan Joker123 Deposit Termurah, Judi Sabung Ayam Bonus 8x Win, Sabung Ayam Live Streaming, Judi Adu Ayam Online, Judi Sabung Ayam Online, Tembak Ikan Online Termurah, Game Ikan Deposit Termurah, Game Ikan Deposit Pulsa, Game Ikan Bonus Deposit, Game Ikan Dengan Jackpot Besar

    Judi Tembak Ikan Online

    bonus besar Sepak Bola

    Bandar judi Sepak Bola

    Judi Tembak Ikan Online

    bonus besar Sepak Bola

    Bandar judi Sepak Bola

    game online uang asli indonesia

    ReplyDelete
  23. Am reușit să-mi iau soțul cu ajutorul unui doctor vrăjitor numit dr Ajayi când am descoperit că a fost fermecat de o altă femeie, am fost foarte fericit că am contactat casta de vrăjitoare pentru că el este motivul pentru care am reușit să-mi câștig înapoi soțul. și acum trăim în pace. Luați legătura cu dr Ajayi pentru toate lucrările dvs. de vrajă, el m-a ajutat să-mi revin husand, cred că vă va ajuta cu ceea ce vă deranjează. E-mail: drajayi1990@gmail.com sau whatsapp: +2347084887094.

    ReplyDelete
  24. Cool you inscribe, the info is really salubrious further fascinating, I'll give you a connect to my scene. usb c to hdmi

    ReplyDelete
  25. Thanks for sharing, nice post! Post really provice useful information!

    Hương Lâm chuyên cung cấp máy photocopy và dịch vụ cho thuê máy photocopy giá rẻ, uy tín TP.HCM với dòng máy photocopy toshiba và dòng máy photocopy ricoh uy tín, giá rẻ.

    ReplyDelete
  26. Sounds Good, I like to read your information about massage. Thanks for sharing this amazing stuff in your blog.Contact us for custom pcb board

    ReplyDelete
  27. Thanks for Sharing.
    Nice information, valuable and excellent information. Save your seat today and live your Career, for more information, website Cornell University & Call 9718417575 for more data/to approach your inquiries and for taking confirmation for Cornell University

    ReplyDelete
  28. If you are stuck with your Management assignment then in this case you can opt for our Management Assignment. we provide the bestOnline marketing expert.We also provideSupply chain Management for students across the globe. for more information contact us +16692714848.


    ReplyDelete
  29. Thank you for sharing such wonderful information.
    There are so many unknown facts and knowledge I have got in your article. I was not aware of these facts yet. However, I have done so much research and written an article on Study Engineering in Cornell University .Call us at 9718417575 or Visit our Website Get Admission Abroad to read that article.

    ReplyDelete
  30. I am thankful to the owner of this beautiful website that provides useful information. I also have a tool that helps you all to generate Random Name I hope you like our tool. Although your article is also important to us. Keep providing such informative content.

    ReplyDelete
  31. Nice Post!!!

    This is really a great article for those who are looking for Discount coupons, cashback offers on online Nokia Mobile Phones Price list shopping sites in India.
    Thanks for sharing the wonderful information and Save you huge money & time. More offers and coupons Visit

    Nokia Mobile Phones Offers and Price list
    Nokia Mobiles Phone Price and Offers
    Nokia Mobiles Phone Price List
    Nokia Mobile Phones offers
    Today Nokia Mobile Phones offers
    Nokia Smart Mobile Phones
    Nokia New Mobiles Model

    Very detailed and informative!!
    Keep sharing...

    ReplyDelete
  32. Nice post, very useful blogs with very useful information, thank you for sharing this post Neck Lift Surgery

    ReplyDelete
  33. Hello every one
    I am new here for this group.
    Study MBBS Abroad 2020 for Indian Students - Overseas Education Consultants in Jaipur, Consult India's best study abroad consultants in Jaipur. Enroll in MCI approved university and get scholarship
    Fujian Medical University
    China Medical University

    ReplyDelete
  34. Hi Everybody
    I am new here this community. I like your article. I am working for DFS Services. we are best service provide.
    Need Home Security System India for Home and Office, Contact DFS Services. Buy High Quality Home Security Intruder Alarm System or Burglar Alarm System, Video Doorbell and Security Cameras (IP Camera). Dfs Services is the best company for Home Security Systems in India.
    home security system Bangalore
    burglar alarm system

    ReplyDelete
  35. Thanks for sharing such beautiful information with us. I hope you will share some more information about Blogspot seo. Please Keep Sharing.

    best artificial intelligence course

    ReplyDelete